The exception is not scary, scary is an exception, you do not know, and do not handle the exception.
Spring MVC Controller exception of the default processing is to respond to a 500 status code, and then display the error message on the page, if the user see this page, you will feel that this site is too low.
To solve the controller's exception problem, of course, you can not add exception handling in each method of processing the request, which is too cumbersome. Spring MVC provides a handlerexceptionresolver interface that can be used for uniform exception handling.
Handlerexceptionresolver interface
publicinterface HandlerExceptionResolver { ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex);}
A resolveexception method is defined in the Handlerexceptionresolver interface to handle exceptions in the controller. The Exception ex parameter is the exception thrown by the controller. The return value type is Modelandview, which can be set by this return value to the page that is displayed when the exception occurs.
Implement Handlerexceptionresolver
Handlerexceptionresolver is a interface, and you need to define an implementation class to implement the logic behind the exception.
public class myexceptionresolver implements handlerexceptionresolver { private Exceptionlogdao Exceptionlogdao; @Override public Modelandview resolveexception (httpservletrequest request, HttpServletResponse R Esponse, Object handler, Exception ex) {//exception handling, such as storing exception information to database Exceptionlo Gdao.save (ex); //view displays a specialized error page Modelandview Modelandview = new modelandview ( " ErrorPage "); return Modelandview; }}
The above code implements the Resolveexception method of the Handlerexceptionresolver class. When an exception occurs, the exception information is stored in the database and a dedicated error page is displayed.
Of course, there are other common exception handling methods, such as sending an exception alert to the relevant person via JavaMail. In short, the anomalies to be seen by the relevant personnel, so as to continuously improve the code.
Configuration
Finally, you will also need to configure your Handlerexceptionresolver implementation class in the spring configuration file, or add the @component annotation.
<bean class="com.xxg.MyExceptionResolver" />
At this point, Myexceptionresolver can handle exceptions thrown by the controller.
What exceptions can handlerexceptionresolver be handled by related issues?
Handlerexceptionresolver can only handle all the exception, that is, the HTTP status code is 500 exception, can not handle 404, 400 and other status code corresponding to the problem.
Will there be a conflict with the error-page configured in Handlerexceptionresolver and Web. xml?
Configuration error-page in Web. XML is also a page that appears when an error is configured:
<error-page> <error-code>500</error-code> <location>/500.jsp</location></error-page>
If Resolveexception returns Modelandview, it will be displayed first based on the page in the return value. However, Resolveexception can return null, at which point the page with the 500 status code configuration of Error-page in Web. XML is displayed.
When there is a corresponding error-page configuration in Web. XML, you can return NULL when implementing the Resolveexception method.
Explanation of the return value in the API documentation:
Return a corresponding modelandview to forward to, or null for default processing.
SPRING MVC controller Unified Exception handling: Handlerexceptionresolver