SpringMVC3 Learning (8) -- Global Exception Handling

Source: Internet
Author: User

In the springMVC configuration file:

 

     
      
   
    
/Error
   
       
      
  
       
   
        
            
             
     
      
500
             
     
      
503
         
        
       
         
            
     
      
Number
     
             
     
      
Null
         
        
   
  
  

The main class here is the SimpleMappingExceptionResolver class, and its parent class AbstractHandlerExceptionResolver class.

 

You can also implement the HandlerExceptionResolver interface to write your own exception handling program.

With SimpleMappingExceptionResolver, we can map different exceptions to different jsp pages (by configuring the exceptionMappings attribute ).

At the same time, we can also specify a default exception prompt page for all exceptions (through the defaultErrorView attribute configuration ),

If the thrown exception does not have a corresponding ing in exceptionMappings, Spring will use this default configuration to display the exception information.

 

Login. java test class

 

Import java. io. file; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. requestMapping; @ Controllerpublic class Login {@ RequestMapping (/null) public void testNullPointerException () {File file = null; // null pointer exception, return the null view System defined in the SpringMVC configuration file. out. println (file. getName () ;}@ RequestMapping (/number) public void testNumberFormatException () {// NumberFormatExcep Tion, return the number view Integer defined in the SpringMVC configuration file. parseInt (abc) ;}@ RequestMapping (/default) public void testDefaultException () {if (1 = 1) // The exception type is not specified in the SpringMVC configuration file, therefore, the default exception view throw new RuntimeException (Error!);} is returned !);}}

Display the error jsp page (error. jsp is used as an example)

 

 

<% Exception e = (Exception) request. getAttribute (exception); out. print (e. getMessage (); %> 

Test URL: http: // localhost: 8080/spring_exception/null

 

 

 

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.