First, the abnormal structure
1. Exceptions from outside to inside are as follows:
The Web server handles exceptions--->web application inside the XML handle exception--->spring framework handles exception----> Controller comment Handling exception
---> Controller method inside try handling exception
Second, for example to explain the implementation method
1.Tomcat Exception Handling: Add error page configuration information to Conf/web.xml. Know
2.WEB application: Project Web. XML Configuration Error page information:
<Error-page> <Error-code>404</Error-code> < Location>/web-inf/jsp/404.jsp</ Location> </Error-page> <Error-page> <Error-code>500</Error-code> < Location>/web-inf/jsp/500.jsp</ Location> </Error-page> <Error-page> <Exception-type>java.lang.Exception</Exception-type> < Location>/web-inf/jsp/error.jsp</ Location> </Error-page>
3.Spring Framework handles Exceptions:
<!--Exception Handler - <Beanclass= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <!--This name property works by: The exception type corresponds to the error page - < Propertyname= "Exceptionmappings"> <Props> <!--The following sentence means: Error in conjunction with the previously configured view processor, locate the ERROR.JSP page when any controller throws RuntimeException, is forwarded to the error.jsp page. You can also add other types of exceptions and the corresponding JSP page, which is the following sentence can be written more than one. The fully qualified name of the custom exception can also be written inside the key. - <propKey= "Java.lang.RuntimeException">Error</prop> <propKey= "Java.lang.IndexOutOfBoundsException">Error1</prop> </Props> </ Property> </Bean>
4. Controller comment Handling Exception:
@ExceptionHandler public String Execute (httpservletrequest req, Exception e) throws Exception { if (E
instanceof
NullPointerException) {Req.setattribu Te ( msg), "Null pointer!" "
);
return "message";
//
forwards to the message page
"
//
can also To increase the if handling of other types of exceptions ...
throw e;
//
}
5. The innermost Try...catch handles exceptions in many ways, such as returning the JSON object to the page handling exception, redirecting the home page address, forwarding to the error page, continuing to throw up, and so on, handling the exception as appropriate.
Spring MVC Exception Handling collation