struts2官方 中文教程 系列八:异常处理
在本教程中,我们将探讨如何启用Struts 2框架处理web应用程序生成的任何未捕获的异常。Struts 2提供了健壮的异常处理,包括能够自动记录任何未捕获的异常,并将用户重定向到错误web页面。
贴个本帖的地址,以免被爬:struts2官方 中文教程 系列八:异常处理 即 http://www.cnblogs.com/linghaoxinpian/p/6915066.html
全局异常处理(Global Exception Handling)
使用Struts 2框架,您可以在Struts.xml中指定框架应该如何处理未捕获的异常。处理逻辑可以应用于所有操作或特定操作。让我们首先讨论如何启用全局异常处理。
为了启用全局异常处理,我们需要在struts.xml中添加以下2个节点,注意插入的位置必须是package节点中的第一位。
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.devMode" value="true" />
<!-- 全局属性文件start -->
<constant name="struts.custom.i18n.resources" value="global" />
<!-- 全局属性文件end -->
<package name="basicstruts2" extends="struts-default">
<!-- 全局异常start -->
<global-results>
<result name="securityerror">/securityerror.jsp</result>
<result name="error">/error.jsp</result>
</global-results> <global-exception-mappings>
<exception-mapping exception="org.apache.struts.register.exceptions.SecurityBreachException" result="securityerror" />
<exception-mapping exception="java.lang.Exception" result="error" />
</global-exception-mappings>
<!-- 全局异常end -->
<action name="index">
<result>/index.jsp</result>
</action>
<!--hello -->
<action name="hello" class="action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action> <!-- register -->
<action name="register" class="action.RegisterAction" method="execute">
<result name="success">/thankyou.jsp</result>
<result name="input">/register.jsp</result>
</action> <!-- registerInput -->
<action name="registerInput" class="action.RegisterAction"
method="input">
<result name="input">/register.jsp</result>
</action> </package> </struts>
对应关系:

在上面的配置中 global exception mapping 节点告诉Struts 2框架,如果应用程序抛出指定的类型(或该类型的子类型)的异常时应该做什么。例如,如果抛出SecurityBreachException ,但未被捕获,则Struts 2 Action类将返回“securityerror”的结果(Result)。所有其他未捕获的异常将导致Struts 2 Action类返回"error"
global results mapping 节点关联结果值(Result)与特定的视图页。例如结果“securityerror”将导致框架将用户的浏览器重定向到securityerror.jsp视图页面。
指定Action类的异常处理
如果您需要以特定的方式处理某个特定Action类的异常,那么您可以在struts.xml中的action节点中映射异常节点,如:
<action name="actionspecificexception" class="org.apache.struts.register.action.Register" method="throwSecurityException">
<exception-mapping exception="org.apache.struts.register.exceptions.SecurityBreachException" result="login" />
<result>/register.jsp</result>
<result name="login">/login.jsp</result>
</action>
对应关系:

如果同一个异常在全局映射也有映射,那么将优先考虑指定Action的异常映射。
日志记录异常(Logging Exceptions)
您可以配置Struts2框架来记录任何未捕获的异常,启用需要在Struts.xml中指定一些参数值。如果您曾看过 ExceptionMappingInterceptor class API,您便知道可以设置三个参数值来启用日志记录(logEnabled)、使用的日志级别(logLevel)和日志类别(logCategory),以方便在日志消息中查看异常。注意,插入位置必须在global-results节点之前,否则会报错。
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.devMode" value="true" />
<!-- 全局属性文件start -->
<constant name="struts.custom.i18n.resources" value="global" />
<!-- 全局属性文件end -->
<package name="basicstruts2" extends="struts-default">
<!-- 启用logging start-->
<interceptors>
<interceptor-stack name="appDefaultStack">
<interceptor-ref name="defaultStack">
<param name="exception.logEnabled">true</param>
<param name="exception.logLevel">ERROR</param>
</interceptor-ref>
</interceptor-stack>
</interceptors> <default-interceptor-ref name="appDefaultStack" />
<!-- 启用logging end--> <!-- 全局异常start -->
<global-results>
<result name="securityerror">/securityerror.jsp</result>
<result name="error">/error.jsp</result>
</global-results> <global-exception-mappings>
<exception-mapping exception="org.apache.struts.register.exceptions.SecurityBreachException" result="securityerror" />
<exception-mapping exception="java.lang.Exception" result="error" />
</global-exception-mappings>
<!-- 全局异常end --> <action name="index">
<result>/index.jsp</result>
</action>
<!--hello -->
<action name="hello" class="action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action> <!-- register -->
<action name="register" class="action.RegisterAction" method="execute">
<result name="success">/thankyou.jsp</result>
<result name="input">/register.jsp</result>
</action> <!-- registerInput -->
<action name="registerInput" class="action.RegisterAction"
method="input">
<result name="input">/register.jsp</result>
</action> <!-- 指定Action类的异常start -->
<action name="actionspecificexception" class="org.apache.struts.register.action.Register" method="throwSecurityException">
<exception-mapping exception="org.apache.struts.register.exceptions.SecurityBreachException" result="login" />
<result>/register.jsp</result>
<result name="login">/login.jsp</result>
</action>
<!-- 指定Action类的异常end --> </package> </struts>
上面的拦截器节点(interceptors)配置了一组名为appDefaultStack的Struts 2拦截器。这一组拦截器基于拦截器的defaultStack(当Struts 2框架调用Action类方法时,它是默认执行的Struts 2拦截器)。
ExceptionMappingInterceptor类是struts2拦截器中的一种,是默认堆栈的一部分,在struts defaultStack的定义中,ExceptionMappingInterceptor 是被给出了异常的名称,通过指定一个带有异常名称的param节点,将log启用...
这句话不怎么看得懂,原话:
The ExceptionMappingInterceptor is one of the Struts 2 interceptors that is part of the default stack. In the definition of the struts defaultStack, the ExceptionMappingInterceptor is given the name of exception. By specifying a param node with the name of exception.logEnabled and a value of true, I’m setting the logEnabled parameter of the ExceptionMappingInterceptor class to true.
现在,当应用程序抛出一个未捕获的异常时,Struts 2框架将处理它,并将为包含堆栈跟踪的信息写入日志(本例中仅输出到控制台,你可以在log4j.xml配置文件中修改)。
在浏览器中显示异常信息
如果您希望使用s:属性标签显示具有异常值和异常堆栈信息:
WebRoot/error.jsp
<h4>应用程序出现了故障。</h4> <p>请用以下信息联系技术支持:</p> <h4>异常名称: <s:property value="exception" /> </h4> <h4>异常详情: <s:property value="exceptionStack" /></h4>
运行:


总结
Struts 2提供了一种易于使用的配置,用于处理未捕获的异常,并将用户重定向到适当的视图页面。您可以将异常处理配置为针对所有操作或仅针对特定操作的全局配置。您还可以启用Struts 2框架来记录未捕获的异常。
struts2官方 中文教程 系列八:异常处理的更多相关文章
- struts2官方 中文教程 系列十四:主题Theme
介绍 当您使用一个Struts 2标签时,例如 <s:select ..../> 在您的web页面中,Struts 2框架会生成HTML,它会显示外观并控制select控件的布局.样式和 ...
- struts2官方 中文教程 系列十三:利用通配符选择方法
介绍 在本教程中,我们将介绍如何在struts.xml中配置action节点以达到仅使用一个action节点将几个不同的url关联到特定action类的特定方法.这样做的目的是减少struts.xml ...
- struts2官方 中文教程 系列十二:控制标签
介绍 struts2有一些控制语句的标签,本教程中我们将讨论如何使用 if 和iterator 标签.更多的控制标签可以参见 tags reference. 到此我们新建一个struts2 web 项 ...
- struts2官方 中文教程 系列十一:使用XML进行表单验证
在本教程中,我们将讨论如何使用Struts 2的XML验证方法来验证表单字段中用户的输入.在前面的教程中,我们讨论了在Action类中使用validate方法验证用户的输入.使用单独的XML验证文件让 ...
- struts2官方 中文教程 系列十:Form标签
介绍 在本教程中,我们将探索其他Struts 2表单控件.在前面的教程中,我们介绍了如何使用Struts 2表单(处理表单.表单验证和消息资源文件),我们介绍了如何使用Struts 2 head, f ...
- struts2官方 中文教程 系列九:Debugging Struts
介绍 在Struts 2 web应用程序的开发过程中,您可能希望查看由Struts 2框架管理的信息.本教程将介绍两种工具,您可以使用它们来查看.一个工具是Struts 2的配置插件,另一个是调试拦截 ...
- struts2官方 中文教程 系列七:消息资源文件
介绍 在本教程中,我们将探索使用Struts 2消息资源功能(也称为 resource bundles 资源绑定).消息资源提供了一种简单的方法,可以将文本放在一个视图页面中,通过应用程序,创建表单字 ...
- struts2官方 中文教程 系列五:处理表单
先贴个本帖的地址,免得其它网站被爬去了struts2教程 官方系列五:处理表单 即 http://www.cnblogs.com/linghaoxinpian/p/6906298.html 下载本章 ...
- struts2官方 中文教程 系列三:使用struts2 标签 tag
避免被爬,先贴上本帖地址:struts2 官方系列教程一:使用struts2 标签 tag http://www.cnblogs.com/linghaoxinpian/p/6901316.html 本 ...
随机推荐
- fstat、stat和lstat 区别(转)
fstat.stat和lstat 区别(转) stat系统调用系列包括了fstat.stat和lstat,它们都是用来返回“相关文件状态信息”的,三者的不同之处在于设定源文件的方式不同. 1 首先隆重 ...
- git 和 github的学习
第一部分:我的github地址 https://github.com/Ly1235/gitLeaming 第二部分:git 和 github Git是一款免费.开源的分布式版本控制系统.gitHub是 ...
- python:常用模块一
一.collections模块 1,在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultd ...
- jmeter报"msg":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported"的解决方法
1.报"msg":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supporte ...
- 负的CPU保护环
什么是CPU保护环? 在计算机科学中, 分级保护域(英语:hierarchical protection domains),经常被叫作保护环(Protection Rings),又称环型保护(Ring ...
- git上传过滤忽略文件
有些时候我们不想把某些文件纳入版本控制中,比如数据库文件,临时文件,设计文件等 在主目录下建立".gitignore"文件,此文件有如下规则: 忽略文件中的空行或以井号(#)开始的 ...
- redis的数据结构与命令
以下部分文档,摘自51cto讲师:汤小洋 redis提供五种数据类型:string,hash,list,set及zset(sorted set). Redis数据就是以key value形式来存储的 ...
- AngularJS 控制器属性
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- android(eclipse)新手常见问题总结(一)
1:jdk无法更新 进入工具里面手动获取镜像资源 并且改为强制 2:报错:This version of the rendering library is more recent than you ...
- React中setState同步更新策略
setState 同步更新 我们在上文中提及,为了提高性能React将setState设置为批次更新,即是异步操作函数,并不能以顺序控制流的方式设置某些事件,我们也不能依赖于this.state来计算 ...