Spring MVC exception Handling

Source: Internet
Author: User
Tags object object

Learn to program spring MVC exception handling is released, welcome to visit via Xuebiancheng8.com

There are two ways to deal with exceptions in Spring3.0: One is to use the handlerexceptionresolver interface, and one is to use it inside the controller class @ExceptionHandler annotations. The first approach enables global exception control, and spring already provides a default implementation class Simplemappingexceptionresolver In the second way, more personalized point exception handling can be implemented inside the controller with greater flexibility. In general, the project only need to adopt the first way, each person to define their own abnormal presentation way, too personality, not unity.

From the current survey results, these two ways can not coexist, do not know whether the future version will be able to integrate them, so as to be flexible configuration.

Exception handling based on the Handlerexceptionresolver interface:

In this way, you only need to implement the Resolveexception method, which returns a Modelandview object, determines the type of the exception inside the method, and then common appropriate Modelandview object, if the method returns NULL, Then spring will continue to look for other beans that implement the Handlerexceptionresolver interface. In other words, spring searches all the beans that have implemented the Handlerexceptionresolver interface in their environment, executing them one after the other, until a Modelandview object is returned.

Example code:

Java code

  1. /**

  2. * Exception handling class based on Handlerexceptionresolver interface

  3. * @author Zywang 2011-4-2

  4. */

  5. public class CustomExceptionHandler implements Handlerexceptionresolver {

  6. @Override

  7. Public Modelandview resolveexception (HttpServletRequest request,

  8. HttpServletResponse response, Object object, Exception Exception) {

  9. If (Exception instanceof IOException) {

  10. return new Modelandview ("Ioexp");

  11. }else if (Exception instanceof SQLException) {

  12. return new Modelandview ("Sqlexp");

  13. }

  14. return null;

  15. }

  16. }

This class must be declared in spring, let Spring manage it, you can use the @component tag, or you can use the <bean/> node. To make exception handling simple, Spring provides the Simplemappingexceptionresolver class, which implements the Handlerexceptionresolver interface, which requires only the use of < The bean/> node is declared, with the following examples:

XML code

  1. <bean class= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" >

  2. <!--define the default exception handling page, which is used when registering the exception type.

  3. <property name= "Defaulterrorview" value= "error" ></property>

  4. <!--defines the name of the variable that the exception handling page uses to get the exception information, which is named exception by default

  5. <property name= "Exceptionattribute" value= "ex" ></property>

  6. <!--define exceptions that require special handling, with the class name or full pathname as key, and the name of the page as the value--

  7. <property name= "Exceptionmappings" >

  8. <props>

  9. <prop key= "IOException" >error/ioexp</prop>

  10. <prop key= "Java.sql.SQLException" >error/sqlexp</prop>

  11. </props>

  12. </property>

  13. </bean>

With Simplemappingexceptionresolver we can map different exceptions to different JSP pages (through the configuration of the Exceptionmappings property), and we can also specify a default XOR for all exceptions.

Often prompt page (through the configuration of the Defaulterrorview property), if the thrown exception does not have a corresponding mapping in Exceptionmappings, spring will display the exception information with this default configuration (note that the exception display interface configured here includes only the main file name, As for the file path and suffix already specified in viewresolver).

A typical exception display page is as follows:
<body>
<% Exception ex = (Exception) request.getattribute ("Exception"); %>
<P/>
<% Ex.printstacktrace (New Java.io.PrintWriter (out)); %>
</body>
Exception is really simplemappingexceptionresolver stored in the request, you can view the source code.
If Simplemappingexceptionresolver cannot meet the need for exception handling, we can
The Handlerexceptionresolver interface implements its own exception-handling class, which is also very simple (it is only necessary to implement a
Resolveexception method).

If there is a viewresolver, then the JSP page must be under that page, then if you can't find the page, you can adjust the page path according to the error

@exceptionhandler-based exception handling:

The method needs to be defined inside the controller, then create a method and decorate it with @exceptionhandler annotations to handle the exception, which is basically the same method as @RequestMapping modification, Only one parameter of type exception can be added, and the type of one or more exceptions @ExceptionHandler, if empty, is considered to trigger all exception type errors.

Example code:

Java code

    1. /** 

    2.  *  based on @exceptionhandler exception handling  

    3.  * @ author zywang 2011-4-2 

    4.  */  

    5. @Controller    

    6. public class exceptionhandlercontroller {  

    7.       

    8.      @ExceptionHandler (value={ Ioexception.class,sqlexception.class})   

    9.     public string  exp (exception ex,httpservletrequest request)  {  

    10.          request.setattribute ("Ex",  ex);   

    11.          return  "/error.jsp";   

    12.      }  

    13.   

    14. }  

Learn to program spring MVC exception handling is released and is welcome to visit via Xuebiancheng8.com.

Spring MVC 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.