2017.3.31 Spring MVC Tutorial (iv) Global exception handling

Source: Internet
Author: User
Tags spring mvc tutorial log4j

Learning Blog: http://elf8848.iteye.com/blog/875830/

Version used in my project: 4.2.0. Blog time earlier, 11, learning is Spring3 MVC. I do not know if there is any change in the version of the larger features.

Spring MVC Tutorial (iv) Global exception handling

1.SimpleMappingExceptionResolver

1 Simplemappingexceptionresolver's parent class: Abstracthandlerexceptionresolver 2 with Simplemappingexceptionresolver, different exceptions are mapped to different JSP pages (through the configuration of the exceptionmappings property).  3 If the thrown exception does not have a corresponding mapping in exceptionmappings, Spring displays the default exception page. The default exception page is configured through the Defaulterrorview property.  4 can also implement the Handlerexceptionresolver interface itself to handle exceptions.

Note : The exception display interface is configured here to include only the main file name , as the file path and suffix are already specified in viewresolver . As/error/error represents/error/error.js.

1 <!--Total error Handling -  2 <BeanID= "Exceptionresolver"class= "Org.springframework.web.servlet.handler." simplemappingexceptionresolver">  3     < Propertyname= "Defaulterrorview">    4         <value>/error/error</value>  5     </ Property>  6     < Propertyname= "Defaultstatuscode">    7         <value>500</value>  8     </ Property>     9     < Propertyname= "Warnlogcategory">    Ten         <value>Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver</value>   One     </ Property>      A </Bean> 

the wrong JSP file is displayed :

"Exception" is a key, which is specified by default in the Simplemappingexceptionresolver class. This value can be modified through a configuration file.

1 <%@ Pagelanguage= "Java"ContentType= "text/html; CHARSET=GBK"  2 pageencoding= "GBK"%>  3 <%@ PageImport= "Java.lang.Exception"%>  4 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">  5 <HTML>  6 <Head>  7 <Metahttp-equiv= "Content-type"content= "text/html; CHARSET=GBK">  8 <title>Error page</title>  9 </Head>  Ten <Body>   One <H1>Something went wrong.</H1>   A <%   - Exceptione= (Exception) request.getattribute ("Exception");  - Out.print (E.getmessage ());  the %>   - </Body>   - </HTML>

2. Logging global exceptions to the log

(1) Warnlogcategory property

1 <  name= "warnlogcategory">           
2 <value>org.springframework.web.servlet.handler.SimpleMappingExceptionResolver </ value > 3 </ Property >

(2) log4j configuration file

Join (fully qualified name): Log4j.logger.org.springframework.web.servlet.handler.simplemappingexceptionresolver=warn. This will certainly be included even if the root log level of the log4j is error.

2017.3.31 Spring MVC Tutorial (iv) 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.