SPRINGMVC Global Exception exception handling Simplemappingexceptionresolver

Source: Internet
Author: User

Inherited the Simplemappingexceptionresolver.

Put code on it.

/*** Global handling of Controller exceptions * distinguishes exception handling for ordinary requests and AJAX requests, normal requests are returned to the configured ErrorCode page, or returned to the specified page *@author *  */ Public classCustomexceptionextendsSimplemappingexceptionresolver {Private Final transientLogger Logger =Loggerfactory.getlogger (GetClass ()); @OverrideprotectedModelandview doresolveexception (httpservletrequest request, httpservletresponse response, Object handler, Exception ex) {String viewName=Determineviewname (ex, request); if(ViewName! =NULL) {//JSP format returned//add normal commit to return to your page ErrorPageString errorpage = string.valueof (Request.getattribute ("ErrorPage")); //go back to your page            if(Stringutils.isnotblank (errorpage)) {ViewName=ErrorPage; }            if(! (Request.getheader ("accept"). IndexOf ("Application/json") >-1 | |(Request. GetHeader ("X-requested-with")! =NULL&&request. GetHeader ("X-requested-with"). IndexOf ("XMLHttpRequest") >-1))) {                //if it is not an asynchronous request//Apply HTTP Status code for error views, if specified. //Only apply it if we ' re processing a top-level request.Integer StatusCode =Determinestatuscode (Request, viewName); if(StatusCode! =NULL) {applystatuscodeifpossible (Request, response, StatusCode); }                returnGetmodelandview (ViewName, ex, request); } Else{//JSON format returns                Try{Map<string, object> jsonmap =NewHashmap<string, object>(); //return is an errorJsonmap.put (Basecontroller.ajax_result,false);                    Jsonmap.put (Basecontroller.result_message, Ex.getmessage ()); Response.setcontenttype ("Text/html;charset=utf-8"); PrintWriter writer=Response.getwriter ();                    Writer.write (json.tojsonstring (Jsonmap));                Writer.close (); } Catch(Exception e) {logger.error ("Doresolveexception", "System exception!" ", E); }                return NULL; }        } Else {            return NULL; }    }}

Spring.xml Configuration

<Beanclass= "Cn.tomcat.quickstart.exception.CustomException">          <!--defines the default exception handling page that is used when registering the exception type -          < Propertyname= "Defaulterrorview"value= "Error"></ Property>          <!--defines the name of the variable that the exception handling page uses to get exception information, by default named Exception -          < Propertyname= "Exceptionattribute"value= "Ex"></ Property>          <!--defines exceptions that require special handling, with the class name or full pathname as key, and the name of the page as the value -          < Propertyname= "Exceptionmappings">              <Props>                  <propKey= "IOException">Error/ioexp</prop>                  <propKey= "Java.sql.SQLException">Error/sqlexp</prop>              </Props>          </ Property>      </Bean>  

SPRINGMVC Global Exception exception handling Simplemappingexceptionresolver

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.