Spring MVC Exception Handling collation

Source: Internet
Author: User

First, the abnormal structure

1. Exceptions from outside to inside are as follows:

The Web server handles exceptions--->web application inside the XML handle exception--->spring framework handles exception----> Controller comment Handling exception
---> Controller method inside try handling exception

Second, for example to explain the implementation method

1.Tomcat Exception Handling: Add error page configuration information to Conf/web.xml. Know

2.WEB application: Project Web. XML Configuration Error page information:

<Error-page>        <Error-code>404</Error-code>        < Location>/web-inf/jsp/404.jsp</ Location>    </Error-page>        <Error-page>        <Error-code>500</Error-code>        < Location>/web-inf/jsp/500.jsp</ Location>    </Error-page>    <Error-page>        <Exception-type>java.lang.Exception</Exception-type>        < Location>/web-inf/jsp/error.jsp</ Location>    </Error-page>

3.Spring Framework handles Exceptions:

<!--Exception Handler -    <Beanclass= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">        <!--This name property works by: The exception type corresponds to the error page -        < Propertyname= "Exceptionmappings">            <Props>                <!--The following sentence means: Error in conjunction with the previously configured view processor, locate the ERROR.JSP page when any controller throws RuntimeException,                    is forwarded to the error.jsp page. You can also add other types of exceptions and the corresponding JSP page, which is the following sentence can be written more than one. The fully qualified name of the custom exception can also be written inside the key.  -                <propKey= "Java.lang.RuntimeException">Error</prop>                <propKey= "Java.lang.IndexOutOfBoundsException">Error1</prop>            </Props>        </ Property>        </Bean>

4. Controller comment Handling Exception:

  @ExceptionHandler  public   String Execute (httpservletrequest req, Exception e)  throws   Exception { if  (E 
   
    instanceof  
     NullPointerException) {Req.setattribu Te ( msg), "Null pointer!"            "
    );  
    return  "message"; 
    // 
     forwards to the message page  
     " 
    // 
     can also To increase the if handling of other types of exceptions ...  
    throw  e; 
    // 
        }
   

5. The innermost Try...catch handles exceptions in many ways, such as returning the JSON object to the page handling exception, redirecting the home page address, forwarding to the error page, continuing to throw up, and so on, handling the exception as appropriate.

Spring MVC Exception Handling collation

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.