Exception handling 将Struts 2异常处理程序映射到操作

Exception handling 将Struts 2异常处理程序映射到操作,exception-handling,struts2,Exception Handling,Struts2,我将Struts 2配置为将任何java.lang.Exception重定向到记录异常的特殊操作。我的重定向工作正常,但我的操作总是得到一个null异常(即使我显式抛出异常)。这是我的struts.xml文件: <global-results> <result name="errHandler" type="chain"> <param name="actionName">errorProcessor</param> <

我将Struts 2配置为将任何java.lang.Exception重定向到记录异常的特殊操作。我的重定向工作正常,但我的操作总是得到一个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>
在throwsException类中,我有以下内容:

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;
   }
}
public String execute() {
     int x = 7 / 0;
     return "success";
}
当我运行程序时,异常处理程序总是得到一个空异常。我正在使用链类型重定向到异常处理程序。我需要实现某种例外软件接口吗?Struts 2异常设置程序是否被称为setException之外的东西


注意:我在编写此程序时试图遵循此步骤。

我遇到的问题与您遇到的问题相同

虽然用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;
}
然后,我可以使用
instanceof
来确保异常
对象
是我期望的类型,然后相应地处理该异常(如将自定义
异常
对象中的消息写入数据库)


让我知道这对你是如何起作用的!:)

使用struts2 version 2.3.1.2,我的错误处理程序中不会出现null异常,一切都按照公布的方式运行。确保您使用的是未修改的defaultStack

就可靠的来源而言,我们可以参考拦截器文件以获取和。ExceptionMappingInterceptor将ExceptionHolder推送到值堆栈上,值堆栈反过来会公开exception属性。链接拦截器将值堆栈上的所有属性复制到目标。由于ExceptionHolder位于堆栈上,如果存在异常设置器,则将对其进行设置

以下是生成工作示例的所有文件:

struts.xml(与问题非常相似):

爆炸(仅抛出RuntimeException):

炸弹视图(/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>

我提供了一个复制链接教程行为的示例,但是它完整地包含了所有必需的文件,以减少出错的可能性。用完整的代码进行了很好的解释!工作起来很有魅力。谢谢对我来说很合适。谢谢
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;
    }
}
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!");
    }
}
<%@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>
<%@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>
INFO: Is exception null: false
INFO: Hello from Exception!