In the SPRINGMVC configuration file:
<bean id= "Exceptionresolver" class= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" > <property name= "Defaulterrorview" > <value>/error</value><!-- Returns a view called error when an exception is thrown but no corresponding exception is found in exceptionmappings </property> <property name= "Defaultstatuscode" V Alue= "404"/><!--represents the default HttpServletResponse return code in the event of an exception, by default 404--> <property name= "Statuscodes" ><!-- Defines the corresponding relationship between the view and the return code in the event of an exception-<props> <!--indicates that the view number is returned when numberformatexception occurs, and then this defines the corresponding htt for the view number when an exception occurs Pservletresponse return code is----<prop key= "number" >500</prop> <prop key= "null" >503</pro p> </props> </property> <property name= "exceptionmappings" > <props> <PR Op key= "numberformatexception" >number</prop><!--means that when the numberformatexception is thrown, it returns a view called number---& Lt;prop key= "NullPointerException" >null</prop> </props> </property></bean>
the basic class here is the Simplemappingexceptionresolver class, and his parent class Abstracthandlerexceptionresolver class.
You can also implement the Handlerexceptionresolver interface and write an exception handler of your own.
By Simplemappingexceptionresolver we are able to map different exceptions to different JSP pages (through the configuration of the Exceptionmappings property).
At the same time we are able to specify a default exception prompt page for all exceptions (via the configuration of the Defaulterrorview property).
Assuming that the thrown exception does not have a corresponding mapping in Exceptionmappings, spring will display the exception information with this default configuration.
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. Returns the null view defined in the SPRINGMVC configuration file System.out.println (File.getname ());} @RequestMapping ("/number") public void Testnumberformatexception () { //NumberFormatException, Returns the number view defined in the SPRINGMVC configuration file integer.parseint ("abc");} @RequestMapping ("/default") public void Testdefaultexception () {if (1 = = 1) //Because the exception type is not specified in the SPRINGMVC configuration file, So it returns the default exception view throw new RuntimeException ("error!");}}
JSP page displaying the error (error.jsp as an example)
<body> <% Exception e = (Exception) request.getattribute ("Exception"); Out.print (E.getmessage ()); %></body>
Test Url:http://localhost:8080/spring_exception/null
Http://localhost:8080/spring_exception/number
Http://localhost:8080/spring_exception/default
Project source code Download: http://download.csdn.net/detail/itmyhome/7382465
SpringMVC3 Learning (eight)--Global exception handling