SPRINGMVC system exception handling and self-defined exception handling

Source: Internet
Author: User

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

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.