Configuring the system exception handler
1. Background simulation of an exception
@RequestMapping (value = "/myexception.do", produces = "Text/html;charset=utf-8") public String myexception () { int a=5/0; return "/error.jsp"; }
2. When the system exception is not configured, the foreground access error 500, after configuring the system exception processor successfully entered the wrong page
<Bean class= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" > <property name= "Defaulterrorview" value= "error.jsp" ></property> <property name= " Exceptionattribute "Value=" ex "></property> </bean>
3. Front Page
Error page ${ex.message}
:
Configuring custom Exception Handlers
1. Create a custom exception class
public class myexception extends exception{ Public MyException () { super (); public MyException (String message) { /span>super (message); }}
public class nameexception extends myexception { public Nameexception () { super (); public nameexception (String message) { super (message); }}
Public class extends myexception { public ageexception () { Super(); } Public ageexception (String message) { Super(message);} }
2. Configuring the Processor method
@RequestMapping (value = "/exception.do", produces = "Text/html;charset=utf-8" public string Exception (string name, Integer age) throws Nameexception, ageexception { if (!name.eq Uals ("admin" throw new nameexception (" User name error " if (age > 40
throw
new ageexception ("Too old" ); return "/result.jsp" ; }
3. Configure Applicationcontext.xml
<!--registering the system exception handler-- <Bean class= " Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver "> <property name=" Defaulterrorview "value=" error.jsp "></property> <property name=" Exceptionattribute "value=" ex "/ > <!--Custom exception handling-- <property name= "exceptionmappings" > <props> <prop key= " Cn.cnsdhzzl.exception.NameException ">/customError/userNameError.jsp</prop> <prop key=" Cn.cnsdhzzl.exception.AgeException ">/customError/userAgeeError.jsp</prop> </props> </property> </bean>
The configuration is completed, and then the exception is entered in the corresponding error page
SPRINGMVC system exception handling and self-defined exception handling