Springmvc the exception that is handled by the Handlerexceptionresolver handler, including handler mappings, data binding, and exceptions that occur when the method executes, The implementation classes for the Handlerexceptionresolver provided by SPRINGMVC are as follows:
Dispatcheservlet the handlerexceptionresolver that are installed by default:
Not using <mvc:annotation-driven/> configuration:
Use of <mvc:annotation-driven/> configuration:
Exceptionhandlerexceptionresolver
It mainly deals with the method defined by @exceptionhandler annotations in handler.
@ExceptionHandler annotation-defined method precedence issues: For example, NullPointerException is occurring, but declared exceptions are runtimeexception and exception, The method that defines the @exceptionhandler annotation that is the least inherited depth is found based on the most recent inheritance of the exception, and the method that marks the runtimeexception.
Package Com.seven.exceptionhandler;import Org.springframework.web.bind.annotation.controlleradvice;import Org.springframework.web.bind.annotation.exceptionhandler;import org.springframework.web.servlet.modelandview;/* * * Created by Hu on 2016/4/4. */@ControllerAdvicepublic class Springmvctestexceptionhandler { @ExceptionHandler {arithmeticexception.class} Public Modelandview handlearithmeticexception (Exception e) { System.out.println ("----> Unexpected:" +e); Go to the view with the logical name error Modelandview modelandview=new modelandview ("error"); Modelandview.addobject ("Exception", e); return modelandview;} }
Responsestatusexceptionresolver
Find the @responsestatus annotation in the parent class of the exception and exception, and then use the annotation's properties for processing.
Package Com.seven.exceptionhandler;import Org.springframework.http.httpstatus;import org.springframework.web.bind.annotation.responsestatus;/** * Created by Hu on 2016/4/4. */@ResponseStatus (value = Httpstatus.forbidden,reason = "username and password do not match!) ") public class Usernamenotmatchpasswordexception extends runtimeexception{ * * When the program occurs usernamenotmatchpasswordexception this exception, * Because this exception is triggered with @responsestatus annotations, So it will be parsed by Responsestatusexceptionresolver, * Then the state of the response is forbidden, the code is 401, no permissions. The reason is "user name and password do not match!" " * Return to Client * * * * }
Defaulthandlerexceptionresolver
Some special exceptions are handled, such as the following exceptions:
Simplemappingexceptionresolver
If you want all exceptions to be handled with consent, 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
<bean class= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" > <!-- Define the exception handling page to get the variable name of the exception information, default to exception--> <property name= "Exceptionattribute" value= "ex" ></property > <property name= "exceptionmappings" > <props> <!--If the following exception occurs, it maps to the error page-- <prop key= "java.lang.ArrayIndexOutOfBoundsException" >error</prop> </props> </ Property> </bean>
Springmvc Exception Handling