struts2官方 中文教程 系列八:异常处理

时间:2023-03-09 02:54:22
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>

对应关系:

struts2官方 中文教程 系列八:异常处理

在上面的配置中 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>

对应关系:

struts2官方 中文教程 系列八:异常处理

如果同一个异常在全局映射也有映射,那么将优先考虑指定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> 

运行:

struts2官方 中文教程 系列八:异常处理

struts2官方 中文教程 系列八:异常处理

总结

Struts 2提供了一种易于使用的配置,用于处理未捕获的异常,并将用户重定向到适当的视图页面。您可以将异常处理配置为针对所有操作或仅针对特定操作的全局配置。您还可以启用Struts 2框架来记录未捕获的异常。