Global Exception Handling of spring MVC and mvc Exception Handling

Source: Internet
Author: User

Global Exception Handling of spring MVC and mvc Exception Handling

1. Use SimpleMappingExceptionResolver to handle exceptions 

Add the following content to the Spring configuration file applicationContext. xml:

<Bean class = "org. springframework. web. servlet. handler. SimpleMappingExceptionResolver"> <! -- Define the default exception handling page and use it when registering this exception type --> <property name = "defaultErrorView" value = "error"> </property> <! -- Defines the variable name used to obtain the exception information on the exception handling page. The default name is exception --> <property name = "exceptionAttribute" value = "ex"> </property> <! -- Define exceptions that require special processing, and use the class name or full path name as the key, the page name is also used as the value --> <property name = "exceptionMappings"> <props> <prop key = "cn. basttg. core. exception. businessException "> error-business </prop> <prop key =" cn. basttg. core. exception. parameterException "> error-parameter </prop> <! -- Here we can continue to extend the processing of different exception types --> </props> </property> </bean>

Note: SimpleMappingExceptionResolver can be used for exception handling. It has the advantages of simple integration, good scalability, and no attention to existing Code. However, this method can only obtain exception information, if an exception occurs, it is not applicable to situations where data except exceptions needs to be obtained.

 

2. Implement the HandlerExceptionResolver interface to customize the exception Processor 

Add the implementation class MyExceptionHandler of the HandlerExceptionResolver interface. The Code is as follows:

Public class MyExceptionHandler implements HandlerExceptionResolver {public ModelAndView resolveException (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {Map <String, Object> model = new HashMap <String, object> (); model. put ("ex", ex); // redirect to different pages based on different errors if (ex instanceof BusinessException) {return new ModelAndView ("error-business", model );} else if (ex instanceof ParameterException) {return new ModelAndView ("error-parameter", model);} else {return new ModelAndView ("error", model );}}}

Add the following content to the Spring configuration file applicationContext. xml:

<bean id="exceptionHandler" class="cn.basttg.core.exception.MyExceptionHandler"/>  

Note: The exception processor that implements the HandlerExceptionResolver interface is used for exception handling, which has the advantages of simple integration, good scalability, and no attention to existing code. At the same time, you can obtain the objects that cause exceptions during exception handling, which is helpful for providing more detailed exception handling information.

 

3. Use the @ ExceptionHandler annotation to handle exceptions 

Added the BaseController class and declared Exception Handling using the @ ExceptionHandler annotation in the class. The Code is as follows:

Public class BaseController {/** Exception Handling Based on @ ExceptionHandler */@ ExceptionHandler public String exp (HttpServletRequest request, Exception ex) {request. setAttribute ("ex", ex); // redirect to different pages based on different errors if (ex instanceof BusinessException) {return "error-business";} else if (ex instanceof ParameterException) {return "error-parameter" ;}else {return "error ";}}}

Modify the code so that all controllers that require exception handling inherit this class,

Note: The @ ExceptionHandler annotation is used to handle exceptions. It has the advantages of simple integration and good scalability (only the Controller class that needs to handle exceptions must inherit the BaseController class) and does not require Spring configuration, however, this method is sensitive to existing code (you need to modify the existing code to make the relevant class inherit from BaseController). During Exception Handling, data other than exceptions cannot be obtained.

 

4. uncaptured Exception Handling 

Modify the web. xml file and add the following content:

<! -- Error page definition --> <error-page> <exception-type> java. lang. throwable </exception-type> <location>/500.jsp</location> </error-page> <error-code> 500 </error-code> <location >/500.jsp</location> </error-page> <error-code> 404 </error-code> <location>/404.jsp</location> </error -page> <! -- Here we can continue to add the server error code processing and corresponding display pages -->

 


How to handle spring mvc exceptions

The SimpleMappingExceptionResolver provided by Spring mvc framework can be used for centralized processing. This class can capture all exceptions thrown by controllers and map them to the configured view.

The usage configuration is as follows:
<Bean id = "exceptionResolver"
Class = "org. springframework. web. servlet. handler. SimpleMappingEx
CeptionResolver ">
<Property name = "defaultErrorView">
<Value> failure </value>
</Property>
<Property name = "exceptionMappings">
<Props>
<Prop key = "java. SQL. SQLException"> showDBError </prop>
<Prop key = "java. lang. RuntimeException"> showError </prop>
</Props>
</Property>
</Bean>

How to configure global exceptions in springmvc

If the code can run, you don't need to worry about the editor.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.