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 本 ...
随机推荐
- 【BZOJ4573】[ZJOI2016] 大森林(LCT)
点此看题面 大致题意: 有\(n\)棵树,初始各有\(1\)个编号为\(1\)的节点,且其为生长节点.\(3\)种操作:将\([l,r]\)区间内的树增加一个新的编号的节点,修改\([l,r]\)区间 ...
- JSON、JSONP、XML的区别
这两天看jquery手册的时候看到了jsonp.发现手册把jsonp与json放在一起讲解了,所以想写篇文章 梳理一下这三者的关系. jsonp: jsonp是json的一种“使用模式”,可以让网页从 ...
- jquery插件layer
//信息框-例1 layer.alert('见到你真的很高兴', {icon: 6}); //信息框-例2 layer.confirm('你确定你很帅么?', {icon: 3}, function( ...
- Javascript文件中的控制器II
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- LeetCode2.两数相加 JavaScript
给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...
- Ajax全接触(2)
例子简介 1.查询员工信息,可以通过输入员工编号查询员工基本信息: 2.新建员工信息,包含员工姓名,员工编号,员工性别,员工职位: 实现: 1.纯html页面,用来实现员工查询和新建的页面: 2.ph ...
- 两台电脑在不同情况下ping的情况
两台计算机(交叉连接) 同一网段 ,可以ping通.不同网段,不可以 两台计算机通过一台交换机连接 同一网段 ,可以ping通.不同网段,不可以.同一网段,同一Vlan,不可以. 综上:跨网段通信,必 ...
- NEC 框架规范 template media
<!DOCTYPE html><html><head><meta charset="utf-8"/><title>NEC ...
- flex 遇到white-space:nowrap
背景,做一个前面图片宽度固定,后面宽度自适应,使用到了flex布局,但是想让后面div里文字不换行,超出以点点表示时,这时布局就乱了,查了下,原来flex布局与white-space:nowrap有影 ...
- 通过命令在navicat中创建数据库及表结构
方法/步骤 首先我们双击打开[navicat]这款软件,在菜单栏中选择[文件]-->[新建连接]-->[MySQL]: 步骤阅读 在打开的[新建连接]对话框中输入[连接名]和[ ...