在本教程中,我们将探讨如何启用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官方 中文教程 系列八:异常处理的更多相关文章

  1. struts2官方 中文教程 系列十四:主题Theme

    介绍 当您使用一个Struts 2标签时,例如 <s:select ..../>  在您的web页面中,Struts 2框架会生成HTML,它会显示外观并控制select控件的布局.样式和 ...

  2. struts2官方 中文教程 系列十三:利用通配符选择方法

    介绍 在本教程中,我们将介绍如何在struts.xml中配置action节点以达到仅使用一个action节点将几个不同的url关联到特定action类的特定方法.这样做的目的是减少struts.xml ...

  3. struts2官方 中文教程 系列十二:控制标签

    介绍 struts2有一些控制语句的标签,本教程中我们将讨论如何使用 if 和iterator 标签.更多的控制标签可以参见 tags reference. 到此我们新建一个struts2 web 项 ...

  4. struts2官方 中文教程 系列十一:使用XML进行表单验证

    在本教程中,我们将讨论如何使用Struts 2的XML验证方法来验证表单字段中用户的输入.在前面的教程中,我们讨论了在Action类中使用validate方法验证用户的输入.使用单独的XML验证文件让 ...

  5. struts2官方 中文教程 系列十:Form标签

    介绍 在本教程中,我们将探索其他Struts 2表单控件.在前面的教程中,我们介绍了如何使用Struts 2表单(处理表单.表单验证和消息资源文件),我们介绍了如何使用Struts 2 head, f ...

  6. struts2官方 中文教程 系列九:Debugging Struts

    介绍 在Struts 2 web应用程序的开发过程中,您可能希望查看由Struts 2框架管理的信息.本教程将介绍两种工具,您可以使用它们来查看.一个工具是Struts 2的配置插件,另一个是调试拦截 ...

  7. struts2官方 中文教程 系列七:消息资源文件

    介绍 在本教程中,我们将探索使用Struts 2消息资源功能(也称为 resource bundles 资源绑定).消息资源提供了一种简单的方法,可以将文本放在一个视图页面中,通过应用程序,创建表单字 ...

  8. struts2官方 中文教程 系列五:处理表单

    先贴个本帖的地址,免得其它网站被爬去了struts2教程 官方系列五:处理表单  即 http://www.cnblogs.com/linghaoxinpian/p/6906298.html 下载本章 ...

  9. struts2官方 中文教程 系列三:使用struts2 标签 tag

    避免被爬,先贴上本帖地址:struts2 官方系列教程一:使用struts2 标签 tag http://www.cnblogs.com/linghaoxinpian/p/6901316.html 本 ...

随机推荐

  1. (LaTex)CTex的初次使用心得及入门教程

    摘要 最近要发论文了,被知乎里人推荐使用论文编译软件(CTex.LaTex和Overleaf之类),瞬间感觉自己用Word简直Out了(书读少). 学校里也听说过LaTex,不过因为当时没怎么写过论文 ...

  2. Scrum第三次冲刺

    1.第三次冲刺任务概述 我们设计的长大万能通系统主要实现以下几个功能: a.周边查询 b.快递代取 c.查看校内新闻动态 d.二手物品交易 第三次冲刺我们计划实现周边查询功能.可以根据评分.距离最近. ...

  3. 浏览器下出现net::ERR_BLOCKED_BY_CLIENT的解决办法

    转发网址:https://www.cnblogs.com/wenzheshen/p/7724065.html 当我们在做开发时,调试页面图片会出现部分图片无法正常显示,并且确认图片的地址正确: 按F1 ...

  4. 03_P52 课后作业

    1. 软件开发的早期阶段为什么进行可行性研究?应该从哪些方面研究系统的可行性? 1.进行可行性研究是为了该软件项目是否值得开发?是否具有经济效益?是否违反法律道德?是否技术可以实现?是否风险性高? 2 ...

  5. django使用orm方式查询mogodb的某段时间的值

    在使用djgango时,需要在数据表中过滤出在某段时间的内容,网上很多或者说Django的orm是针对mysql,且字段类型是datetime或者其他时间类型,使用__rang这个函数就可以查询某个时 ...

  6. HDU 3038 How Many Answers Are Wrong 【YY && 带权并查集】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 2000/1000 ...

  7. 2018.11.29 Struts2中拦截器的学习&项目的实际运用

    struts2官方架构 拦截器的创建 第一种方式 第二种方式 看源码AbstractInterceptor 底层已经帮我们写过这些方法了 第三种方式(推荐) 拦截器API学习 放行 前后处理 不放行, ...

  8. 【模板】各种背包问题&讲解

                                        背包问题集合 一般来说,动态规划(DP)都是初学者最难闯过的一关,而在这里详细解说动态规划的一种经典题型:背包问题. 这里介绍的 ...

  9. POJ 2533 Longest Ordered Subsequence(裸LIS)

    传送门: http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 6 ...

  10. Ueditor上传图片到本地改造到上传图片到七牛云存储

    作为新手说多了都是泪啊!我特别想记录一下作为菜鸟时的坑.看看以后是否会看着笑出来. 为什么要改到云存储上就不说了.好处多多. 视频教程上使用的又拍云同时也提到了七牛云.下来我自己也查了下.又拍云是试用 ...