我配置了Struts 2,将任何java.lang.Exception重定向到记录异常的特殊Action。我的重定向工作了,但是我的Action总是得到一个空异常(即使我显式抛出一个异常)。下面是我的struts.xml文件:
<global-results>
<result name="errHandler" type="chain">
<param name="actionName">errorProcessor</param>
</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="errHandler" />
</global-exception-mappings>
<action name="errorProcessor" class="myErrorProcessor">
<result name="error">/error.jsp</result>
</action>
<action name="throwExceptions" class="throwExceptions">
<result name="success">done.jsp</result>
</action>
在我的错误处理程序中,我有以下内容:
public class myErrorProcessor extends ActionSupport {
private Exception exception;
public String execute() {
System.out.println("null check: " + (exception == null));
return "error";
}
public void setException(Exception exception) {
this.exception = exception;
}
public Exception getException() {
return exception;
}
}
在throwsException类中,我有以下内容:
public String execute() {
int x = 7 / 0;
return "success";
}
当我运行程序时,异常处理程序总是得到一个空异常。我使用链类型重定向到异常处理程序。我需要实现某种类型的ExceptionAware接口吗?struts2异常设置器是叫什么除了setException?
注意:我在编写这个程序时试图遵循这个教程。
使用struts2版本2.3.1.2,我在错误处理程序中没有得到空异常,一切都像宣传的那样工作。确保使用未修改的defaultStack。
就可靠的来源而言,我们可以参考ExceptionMappingInterceptor和Chaining interceptor的拦截器文档。ExceptionMappingInterceptor将ExceptionHolder推入值堆栈,从而暴露一个异常属性。链接拦截器将值堆栈上的所有属性复制到目标。由于ExceptionHolder在堆栈上,如果有Exception的setter,它将被设置。
下面是生成工作示例的所有文件:
struts.xml(与问题保持非常相似):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple" />
<package name="kenmcwilliams" namespace="/" extends="struts-default">
<global-results>
<result name="errHandler" type="chain">
<param name="actionName">errorProcessor</param>
</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="errHandler" />
</global-exception-mappings>
<action name="errorProcessor" class="com.kenmcwilliams.test.myErrorProcessor">
<result name="error">/WEB-INF/content/error.jsp</result>
</action>
<action name="throwExceptions" class="com.kenmcwilliams.kensocketchat.action.Bomb">
<result name="success">/WEB-INF/content/bomb.jsp</result>
</action>
</package>
</struts>
MyErrorProcessor.java
package com.kenmcwilliams.test;
import com.opensymphony.xwork2.ActionSupport;
public class MyErrorProcessor extends ActionSupport {
private Exception exception;
@Override
public String execute() {
System.out.println("Is exception null: " + (exception == null));
System.out.println(""
+ exception.getMessage());
return "error";
}
public void setException(Exception exceptionHolder) {
this.exception = exceptionHolder;
}
public Exception getException() {
return exception;
}
}
炸弹(只抛出RuntimeException):
package com.kenmcwilliams.kensocketchat.action;
import com.opensymphony.xwork2.ActionSupport;
public class Bomb extends ActionSupport{
@Override
public String execute() throws Exception{
throw new RuntimeException("Hello from Exception!");
}
}
查看炸弹(/WEB-INF/content/bomb.jsp) [Never reachable]
<%@taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>The Bomb!</title>
</head>
<body>
<h1>The Bomb!</h1>
</body>
</html>
查看错误(/WEB-INF/content/error.jsp)
<%@taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Global Error Handler</title>
</head>
<body>
<h1>Global Error Handler</h1>
<s:property value="exception.stackTrace"/>
</body>
</html>
输出:我看到error.jsp render,我看到以下内容打印在glassfish控制台:
INFO: Is exception null: false
INFO: Hello from Exception!
我遇到了和你一样的问题!
虽然让Struts填充exception
字段要干净得多,但似乎没有发生这个字段填充(如您所述)。为了解决这个问题,我从异常处理程序类(您的myErrorProcessor
类)中删除了异常字段(及其getter/setter),并将其替换为"手动"从ValueStack
中提取异常的方法:
/**
* Finds exception object on the value stack
*
* @return the exception object on the value stack
*/
private Object findException() {
ActionContext ac = ActionContext.getContext();
ValueStack vs = ac.getValueStack();
Object exception = vs.findValue("exception");
return exception;
}
然后我可以使用instanceof
来确保异常Object
是我所期望的类型,然后相应地处理该异常(如将自定义Exception
对象中发现的消息写入数据库)。
让我知道这对你是如何起作用的!:)