The Web layer framework unifies all exceptions in a Web site

Source: Internet
Author: User

The exception of a website as a professional person, is not easily exposed to the user, because it is not safe, it seems that you are missing is one thing, as long as the site to consider the data security issues, the following for you to share some common web layer framework is how to handle the unified exception.

Before all is in the STRUTS2 configuration file to configure the interceptor to uniformly handle all the exceptions in the system, to everyone with a bit of pseudo-code it, at least once used, hehe.

The configuration file configuration information is as follows:

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

 1  <package name= "base"  extends= "Struts-default" > 2          <!--  Exception handling  --> 3          <interceptors> 4              <!-- 1. Declaring custom Exception interceptors  --> 5              <interceptor name= "Exceptioninterceptor"  class= " Com.it.yycg.framework.exception.ExceptionInterceptor "></interceptor> 6              <!-- 2. Declaring Interceptor Stacks   --> 7              <interceptor-stack name= " Customstack "> 8                  <interceptor-ref name= "exceptionInterceptor "/> 9                  <interceptor-ref name= "Defaultstack"/>10              </interceptor-stack>11          </interceptors>12         <!--  Set the default interceptor to execute  -->13         <default-interceptor-ref  name= "Customstack"/>14         <!--  public   result -->15         <global-results>16              <result name= "error_jsp"  >/ common/error.jsp</result>17              <!--  Deny access back page &NBSP;--&GT;18             <result name= "Refuse " >/common/refuse.jsp</result>19         </ global-results>20         21          <!--  Configure access action returns the label of the JSP page  22              struts executes the Execute () method in the class Actionsupport by default 23              class default actionsupport24               return after success name defaults to success25          -->26         <action name= "Ui_*_*" >27 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&LT;RESULT&GT;/BUSINESS/{1}/{2}. Jsp</result>28         </action>        29      </package>

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

The Java class pseudo-code is as follows:

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

          ExceptionInterceptor           serialVersionUID = -3570177760250354837L        string intercept (actioninvocation actioninvocation)            HttpServletRequest request =          String result =                                      result =         }                                            exceptionresultinfo exceptionresultinfo =                               (e                   exceptionResultInfo =              }    (e  unknownaccountexception | |  e                   resultinfo resultinfo = resultutil.createfail ("User name or password is wrong! "                  exceptionresultinfo =               }    (e                   resultinfo resultinfo = resultutil.createfail ("No permission! "                  exceptionresultinfo =

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

First of all, there are three ways I know about SPRINGMVC handling uniform anomalies: directly using the exception handler provided by SPRINGMVC, manually configuring your own custom global exception handler and using annotations (which is not used).

Let's start with the first one, which is to configure the simple exception handler provided by the SPRINGMVC itself directly in the core configuration file of Springmvc.

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

 1  <!--&NBSP;SPRINGMVC provides simple exception handlers  --> 2     <!--  <bean class= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" > 3            define the default exception handling page  4          <property name= "Defaulterrorview"  value= "/web-inf/jsp/exception.jsp"/>                                       5           defines the name of the variable that the exception handling page uses to get exception information, or it can be undefined, by default named Exception  6          <property name= "ExceptionAttribute"  value= " Ex "/> 7          defines exceptions that require special handling, which is important point   8      &Nbsp;   <property name= "Exceptionmappings" > 9              <props>10                  <prop key= " Com.tgsit.cjd.model.CustomException ">exception</prop>11              </props>12               can also define additional custom exceptions 13         </property> 14     </bean> -->

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

  The second is to define an exception handler yourself, and then configure it by configuring it in the SPRINGMVC core configuration file.

The pseudo-code for the custom exception handler class is as follows:

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

 1 /** 2  *  Custom Global Exception handler: 3  */ 4 public class  customexceptionresolver implements handlerexceptionresolver { 5       @Override  6     public modelandview resolveexception ( Httpservletrequest request, httpservletresponse response, object handler, 7 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;EXCEPTION&NBSP;EX)  { 8           9          ex.printstacktrace ();10         customexception  customexception = null;11         //Direct Conversions If a custom exception is thrown 12          if (ex instanceof customexception)  {13              customexception =  (customexception)  ex;14          } else {15              //if a non-custom exception is thrown, reconstruct an unknown error exception 16              new sysexception ("System is in maintenance, please try again later ...");17          }18         //returns an error message to the foreground 19          modelandview modelandview = new modelandview (" Exception "); 20         modelandview.addobject (" Message ",  Customexception.getmessage ()); 21         return modelandview ; 22     }23 }

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

The SPRINGMVC profile configuration information is as follows (please remember to change the package name when you have to copy it in case of necessity):

1 <!--Configure a custom global exception handler as long as the Handlerexceptionresolver interface is implemented as a global exception handler-->2 <bean class= " Com.tgsit.cjd.utils.CustomExceptionResolver "/>


The Web layer framework unifies all exceptions in a Web site

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.