在Action中execute方法声明为:public String execute() throws Exception,这样,Action可以抛出任何Exception。
1)自己实现异常处理
我们还以helloWorldAction为例,在Action的execute方法中这样写:
public String execute() throws Exception { int a=5/0; this.businessExecute(); return "toWelcome"; }
上面的代码中有int a=5/0;,很显然,会抛出除数为0的错误,这个错误是RuntimeException,我们的程序没有进行相应的例外处理,则会抛给Struts2去处理。那么,在实际的项目中很显然不能这么简单而粗暴的处理错误,一种简单的处理方法就是跳转到一个错误处理页面。
假设要求这个Action,在出现“ArithmeticException”的时候,跳转到一个叫math-exception的Result,而其他错误跳转到另一个叫otherwise-exception的Result。那么,在Action中可以这么写:
public String execute() throws Exception { try { int a = 5/0; } catch (ArithmeticException e) { e.printStackTrace(); return "math-exception"; } catch (Exception e){ e.printStackTrace(); return "otherwise-exception"; } this.businessExecute(); return "success"; }
这样,在运行中出现ArithmeticException就会跳转到math-exception指定的页面,而其他Exception就会跳转到otherwise-exception指定的页面,如果没有出错,就会跳转到success指定的页面。在struts.xml的Action中,只要配置好上述三个Result就可以正常运行了。
2)Struts2的异常处理机制
在<action>元素中设置<exception-mapping>元素,可以指定在execute方法抛出指定错误的时候,跳转到哪个指定的页面。用Struts2的异常机制就不需要自己手动去try-catch了。action里的代码如下:
public String execute() throws Exception { int a=5/0; this.businessExecute(); return "toWelcome"; }
然后在struts.xml的<action>元素中增加<exception-mapping>子元素,示例如下:
<action name="helloworldAction" class="cn.javass.hello.struts2impl.action.HelloWorldAction"> <exception-mapping result="math-exception" exception="java.lang.ArithmeticException"/> <exception-mapping result="math-exception" exception="java.lang.Exception"/> <result name="math-exception">/${folder}/error.jsp</result> <result name="toWelcome">/${folder}/welcome.jsp</result> <result name="input">/${folder}/login.jsp</result> </action>
在<action>元素里面,增加了两个<exception-mapping>元素,其execption属性指定了一个Exception的全类名,如果Action的execute方法抛出的错误是这个Exception类的实例或其派生类的实例,则会跳转到对应的result属性所指定的结果。当然,它们指定的名称为“math-exception”的Result,还是需要另行配置的。
3)全局异常映射
上面的<exception-mapping>元素是作为<action>元素的子元素来配置的,只对本<action>元素有效。其实跟局部Result和全局Result一样,也可以把<exception-mapping>提到父包中,做成全局的异常映射。
全局异常映射仍然是<exception-mapping>元素,只不过不再是<action>元素的子元素,而是<global-exception-mappings>元素的子元素,而<global-exception-mappings>元素是<package>元素的子元素。当然,配置了<global-exception-mappings>,自然需要配置<global-results>,而且<global-results>还必须在<global-exception-mappings>之前,示例如下:
<package name="helloworld" extends="struts-default"> <global-results> <result name="math-exception">/${folder}/error.jsp</result> </global-results> <global-exception-mappings> <exception-mapping result="math-exception" exception="java.lang.ArithmeticException"/> <exception-mapping result="math-exception" exception="java.lang.Exception"/> </global-exception-mappings> <action name="helloworldAction" class="cn.javass.hello.struts2impl.action.HelloWorldAction"> <result name="toWelcome">/${folder}/welcome.jsp</result> <result name="input">/${folder}/login.jsp</result> </action> </package>
对比局部结果和全局结果的查找顺序,可以很容易的理解局部异常映射和全局异常映射的查找顺序:
注意:如果出现同样符合条件的异常映射,上述的顺序也是异常映射之间的优先顺序,也就是说,如果Action的execute方法抛出一个异常,而局部异常映射和全局异常映射中都有相应的配置,那会以局部异常映射为准。
4)在页面输出异常信息
在前面的示例中,当Action的execute方法抛出一个异常之后,跳转到指定的页面,也就是error.jsp后,并没有把具体的异常信息展示出来。那么,想要在error.jsp页面上展示Exception的错误信息,该怎么做呢?
可以使用Struts2提供的标签,来输出Exception的错误信息,如下:
error.jsp的页面如下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <font color=red><b>对不起,出错了,错误信息为:</b></font><br> <s:property value="exception"/> <br> <font color=red><b>错误的堆栈信息为:</b></font><br> <s:property value="exceptionStack"/> </body> </html>
按照上面的例子,会输出以下错误:
1 对不起,出错了,错误信息为: 2 java.lang.ArithmeticException: / by zero 3 错误的堆栈信息为: 4 java.lang.ArithmeticException: / by zero at cn.javass.hello.struts2impl.action.HelloWorldAction.execute(HelloWorldAction.java:12) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450) at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252) at org.apache.struts2.interceptor.DeprecationInterceptor.intercept(DeprecationInterceptor.java:41) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265) at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:139) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:562) at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Thread.java:619)
参考资料:http://www.iteye.com/topic/1124526
【struts2】Struts2的异常处理,布布扣,bubuko.com
原文:http://www.cnblogs.com/ningvsban/p/3735370.html