在本教程中,我们将探讨如何启用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. Jmeter入门19 保存测试结果(或从文件读取结果)

    以聚合报告为例,其他监听器有write results to file的类似. 首先 为了避免每次保存的测试报告被覆盖,我们在testplan下添加两个参数:项目名和当前时间(毫秒级) 其次 添加聚合 ...

  2. Android(java)学习笔记44:Map集合的遍历之键值对对象找键和值

    1. Map集合的遍历之 键值对对象找 键和值: package cn.itcast_01; import java.util.HashMap; import java.util.Map; impor ...

  3. UOJ 35 后缀数组

    后缀数组裸题,求排名第1~n的后缀,想相邻后缀的最长公共前缀. 集训队模板就是硬lO(∩_∩)O哈哈~ #include <cstdio> #include <cmath> # ...

  4. ASP.NET MVC有用工具

    Route Debugger https://www.nuget.org/packages/routedebugger 在Asp.Net MVC程序中,路由(Route)是一个非常核心的概念,可以说是 ...

  5. 【luogu T24743 [愚人节题目5]永世隔绝的理想乡】 题解

    题意翻译 我们来说说王的故事吧. 星之内海,瞭望之台.从乐园的角落告知汝等.汝等的故事充满了祝福.只有无罪之人可以进入——『永世隔绝的理想乡(Garden of Avalon)』! 题目背景 zcy入 ...

  6. C#基础 一(方法详解)

    需要知道:类和方法的关系 方法和参数修饰符 自定义方法可以有或没有参数,也可以有或没有返回值.可以被各种关键字(static.virtual.public.new等)修饰以限制其行为. C#参数修饰符 ...

  7. Windows无法安装到这个磁盘 选中的磁盘具有MBR分区表解决方法

    在安装 win10的时候,会出现这种提示:Windows 无法安装到这个磁盘.选中的磁 盘具有 MBR 分区表.在 EFI 系统上, Windows 只能安装到 GPT 磁盘.出现这种 情况主要是因为 ...

  8. django-多表操作2

    #######多表操作二######## 昨天写了基于双下划线查找,都是两个表之间查找,那再多跨几个表呢?还是一样,一步一步分析 # 跨多表查询: 查询红楼梦这本书的作者的电话: (Author,Bo ...

  9. BZOJ1030: [JSOI2007]文本生成器(AC自动机)

    Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 5984  Solved: 2523[Submit][Status][Discuss] Descripti ...

  10. VS中R转义字符处理

    std::string s1 = R"(Name="Hello World ... ")"; std::string s2 = R"-(Name=&q ...