Dav*_*vid 12 exception-handling struts2
我已将Struts 2配置为将任何java.lang.Exception重定向到记录异常的特殊Action.我的重定向工作,但我的Action总是得到一个null异常(即使我明确抛出一个异常).这是我的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>
Run Code Online (Sandbox Code Playgroud)
在我的错误处理器中,我有以下内容:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
在throwsException类中,我有以下内容:
public String execute() {
int x = 7 / 0;
return "success";
}
Run Code Online (Sandbox Code Playgroud)
当我运行我的程序时,异常处理程序总是获得一个空异常.我正在使用链类型重定向到异常处理程序.我是否需要实现某种ExceptionAware接口?Struts 2异常setter是否除了setException之外还调用了什么?
注意:我在编写此程序时试图遵循本教程.
使用struts2版本2.3.1.2我的错误处理程序中没有得到null异常,一切都按照宣传的方式工作.确保使用未修改的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>
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
炸弹(只是抛出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!");
}
}
Run Code Online (Sandbox Code Playgroud)
查看炸弹(/WEB-INF/content/bomb.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>The Bomb!</title>
</head>
<body>
<h1>The Bomb!</h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
查看错误(/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>
Run Code Online (Sandbox Code Playgroud)
输出:
我看到error.jsp渲染,我在glassfish控制台上看到以下内容:
INFO: Is exception null: false
INFO: Hello from Exception!
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题!
虽然exception使用Struts填充字段要清晰得多,但似乎没有发生这种字段填充(正如您所说).为了解决这个问题,我从我的异常处理程序类(您的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;
}
Run Code Online (Sandbox Code Playgroud)
然后,我可以使用instanceof以确保异常Object是我期望的类型,然后相应地处理该异常(例如将在自定义Exception对象中找到的消息写入数据库).
让我知道这对你有用!:)
| 归档时间: |
|
| 查看次数: |
10553 次 |
| 最近记录: |