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