Exception Handling for SPRINGMVC

Source: Internet
Author: User

First add the <mvc:annotation-driven> tag to the SPRINGMVC:

<mvc:annotation-driven></mvc:annotation-driven>

Create a new error.jsp page

1. Annotation-based exception handling

(1)@ExceptionHandler annotations

Defines methods that are decorated by @ExceptionHandler annotations in the current Handler to handle the exception information in the current Handler !

The following methods are defined in handler:

1@ExceptionHandler (value={arithmeticexception.class})2  PublicString handleexception () {3System.out.println ("exceptionhandler====arithmeticexception=======");4     return"Error";5 }6@ExceptionHandler (value={exception.class})//value for the corresponding exception information, the exception byte-code file, can define multiple 7  PublicString HandleException1 () {8System.out.println ("exceptionhandler====arithmeticexception=======");9     return"Error";Ten } One@RequestMapping (value= "/test", method=requestmethod.get) A  PublicString testexception (@RequestParam (value= "I") (Integer i) { -     intj= 10/i; -     return"Success"; the}

Success returns SUCCESS.JSP, and failure returns error.jsp

Attention:
@ExceptionHandler method decorated in the parameter can be added to the Exception type of parameters, which corresponds to the occurrence of the exception information
@ExceptionHandler method cannot be passed into the map or use model. If you want to upload the exception information to the page, you need to use Modelandview as the return value of the method.

 1  @ExceptionHandler (value={arithmeticexception. Class  })  2  public   Modelandview handleexception (Exception ex) { 3  System.out.println ("exceptionhandler====arithmeticexception======="  4  modelandview mv=new   Modelandview ();  5  mv.addobject ("ex" , ex);  6  mv.setviewname ("error"  7  return   MV ; 8 } 

Get the return value on the error.jsp page with ${ex}. java.lang.ArithmeticException:/by Zero

Priority issues

① when there are two exception handling, the precedence is accurate, for example, the above defines an exception,

arithmeticexception and  Exception

When the arithmeticexception anomaly occurs, the processing method of ArithmeticException is prioritized, and the output

exceptionhandler====arithmeticexception=======

② if in the method defined above, change arithmeticexception.class to runtimeexception. class, which is the declaration of RuntimeException and Exception, at which time arithmeticexception occurs ,

The one with the deepest inheritance depth is found based on the most recent inheritance of the exception, which is the exception handling method that will walk runtimeexception .

Note: (if the arithmeticexception replaced with NullPointerException, at this time in the occurrence of ArithmeticException, will walk exception processing method, and the depth of inheritance is related, did not understand good)

(2)@ControllerAdvice annotations

Custom Handler classes, all exceptions to the Handler class go through the methods in this class

1 @ControllerAdvice2  Public classMyException {3@ExceptionHandler (value=exception.class)4      PublicString handleexception () {5System.out.println ("controlleradvice====exception=======");6         return"Error";7     }8@ExceptionHandler (value={arithmeticexception.class})9      PublicString handleexception () {TenSystem.out.println ("controlleradvice====arithmeticexception======="); One         return"Error"; A     } -}

The issues and priorities that need attention are the same as in (1).

It is also important to note that when two cases (1) (2) coexist, an exception takes precedence over the exception handling method in this class.

2. Configuration-based exception handling

If you want to uniformly handle all exceptions, you can use simplemappingexceptionresolver, which maps the exception class name to the view name, that is, the exception is reported with the corresponding view when an exception occurs

Configuring in Springmvc

1 <Beanclass= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">2     <!--specifies the key required to get exception information in the request domain: Ex, default is exception - 3     < Propertyname= "Exceptionattribute"value= "Ex"></ Property>4      <!--Specifying Exception Mappings - 5     < Propertyname= "Exceptionmappings">6         <Props>7             <!--The key property of the prop tag specifies the full class name of the exception that occurs, specifying the page to which the exception occurs by value!  -8             <propKey= "Java.lang.ArithmeticException">Error</prop>9         </Props>Ten     </ Property> One </Bean>

You can get the exception name on the error.jsp page with ${ex}. java.lang.ArithmeticException:/by Zero

Annotation-based is higher than configuration-based precedence.

Exception Handling for SPRINGMVC

Related Article

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.