SpringMVC3 Learning (eight)--Global exception handling

Source: Internet
Author: User

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> &LT;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

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.