问题描述
在做struts1.x全局异常捕获的时候遇到一个问题:1.当我的类LoginAction中写execute方法时,能捕获到自定义的异常并跳转到专门处理错误的页面显示错误信息,publicclassLoginActionextendsBaseAction{privateBaseservicebaseservice;publicActionForwardexecute(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse)throwsAppException{LoginFormloginform=(LoginForm)form;System.out.println("login==========================");try{baseservice.addvalue();}catch(Exceptione){thrownewAppException("error.add","值出错");//抛出自己定义的异常类异常}returnmapping.findForward("success");}
BaseAction继承自DispatchActionpublicclassBaseActionextendsDispatchAction{protectedLoglogger;publicBaseAction(){logger=LogFactory.getLog(this.getClass());}protectedActionForwarddispatchMethod(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse,Stringname){System.out.println("name===="+name);if(name==null){try{returnthis.unspecified(mapping,form,request,response);}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}Methodmethod=null;try{method=getMethod(name);//System.out.println("method=========="+method);}catch(NoSuchMethodExceptione){//Stringmessage=messages.getMessage("dispatch.method",mapping//.getPath(),name);//logger.error(message,e);//StringuserMsg=messages.getMessage("dispatch.method.user",//mapping.getPath());//thrownewNoSuchMethodException(userMsg);}ActionForwardforward=null;StringnoToken=request.getParameter("_noToken");//System.out.println("noToken=============="+noToken);if(!"1".equals(noToken)){if(!checkToken(request)){//如果重复提交//System.out.println("22==============");thrownewAppException("err.add","页面不能做刷新操作(或者页面已经提交,请查看数据确认)");}}else{logger.debug("noToken");}Objectargs[]={mapping,form,request,response};try{forward=(ActionForward)method.invoke(this,args);}catch(IllegalArgumentExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(IllegalAccessExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(InvocationTargetExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}//ActionForwardforward=null;returnforward;}
但是当我的LoginAction中写上其他方法名的时候就不能正常跳转到处理错误页面进行处理了下面是我的配置文件<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEstruts-configPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.1//EN""http://struts.apache.org/dtds/struts-config_1_1.dtd"><struts-config><form-beans><form-beanname="loginform"type="com.sitech.form.LoginForm"/></form-beans><global-exceptions><exceptionkey="error.exception"type="com.sitech.framework.exception.AppException"handler="com.sitech.framework.exception.AppExceptionHandler"scope="request"path="/term/errorpage_undefine.jsp"/></global-exceptions><action-mappings><actionpath="/login"name="loginform"parameter="operate"scope="request"type="org.springframework.web.struts.DelegatingActionProxy"><forwardname="success"path="/success.jsp"></forward><forwardname="failure"path="/failure.jsp"></forward></action></action-mappings><message-resourcesparameter="MessageResources"/><plug-inclassName="org.springframework.web.struts.ContextLoaderPlugIn"><set-propertyproperty="contextConfigLocation"value="/WEB-INF/a*.xml"/></plug-in></struts-config>
我的异常类定义:publicclassAppExceptionHandlerextendsExceptionHandler{publicActionForwardexecute(Exceptionex,ExceptionConfigae,ActionMappingmapping,ActionFormformInstance,HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException{System.out.println("execute=========");ActionForwardforward=null;ActionMessageerror=null;Stringproperty=null;if(ae.getPath()!=null){//配置文件中的pathforward=newActionForward(ae.getPath());}else{forward=mapping.getInputForward();//如果没有找到path,转到input配置的路径}this.logException(ex);//处理自定义的异常类SystemExceptionif(exinstanceofAppException){AppExceptionse=(AppException)ex;//如果只有message,而没有keySystem.out.println("ex======"+se.getKey());if(se.getKey()==null){error=newActionMessage(ae.getKey(),ex.getMessage());property=ae.getKey();}else{//中有key值error=newActionMessage(se.getKey(),se.getValues());property=se.getKey();}request.setAttribute(Globals.EXCEPTION_KEY,ex);this.storeException(request,property,error,forward,ae.getScope());returnforward;}returnsuper.execute(ex,ae,mapping,formInstance,request,response);}
自定义异常类publicclassAppExceptionextendsRuntimeException{privateStringkey;//得到本地资源文件keyprivateObject[]values;publicAppException(){super();}publicAppException(Stringmessage,Throwablearg1){super(message,arg1);}publicAppException(Stringmessage){super(message);}publicAppException(Throwablemessage){super(message);}publicAppException(Stringkey,Stringmessage,Throwablearg1){super(message,arg1);this.key=key;}publicAppException(Stringkey,Objectvalue){super();this.key=key;values=newObject[]{value};}publicAppException(Stringkey,Objectvalue1,Objectvalue2){super();this.key=key;values=newObject[]{value1,value2};}publicAppException(Stringkey,Object[]values){super();this.key=key;this.values=values;}publicStringgetKey(){returnkey;}publicObject[]getValues(){returnvalues;}}