exception - 为什么从我的拦截器引发的异常没有被 <global-exception-mappings> 捕获?

标签 exception struts2 struts-config struts2-interceptors struts2-convention-plugin

我有一个自定义拦截器,我从中抛出异常;

拦截器运行的操作由约定插件管理;

拦截器引发的异常是在 Action 运行到的包的 struts.xml 中全局定义的。

结果:异常映射被忽略,我得到了

Struts Problem Report

Struts has detected an unhandled exception:

...

Stacktraces

java.lang.IllegalArgumentException: my message

我想我只是错过了一些愚蠢的东西......我们已经讨论过这个 in a similar question ,但目前还不清楚是否可以这样工作:

struts.xml

<package name="my-package" namespace="my" extends="struts-default">
    <interceptors>

        <interceptor name="myInterceptor" class="foo.bar.MyInterceptor"/>

        <interceptor-stack name="myStack">
            <interceptor-ref name="myInterceptor"/>
            <interceptor-ref name="defaultStack"/>
        </interceptor-stack>

    </interceptors> 

    <default-interceptor-ref name="myStack"/>

    <global-results>
        <result name="input">/WEB-INF/content/my-input.jsp</result>
        <result name="error">/WEB-INF/content/my-error.jsp</result>
    </global-results>

    <global-exception-mappings>
        <exception-mapping exception="java.lang.IllegalArgumentException" 
                              result="error" />
        <exception-mapping exception="java.lang.Exception" result="error" />
    </global-exception-mappings>    
</package>

行动

@ParentPackage("my-package")
@Namespace("/my/blabla/yadayada")
public class MyAction extends MyBaseAction {

}

拦截器

@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
    // ....
    if (somethingWrong) {
        throw new IllegalArgumentException("All work and no play makes Jack a dull boy");
    }
}

我还尝试在抽象 BaseAction 或物理实际 Action 本身中定义全局结果/全局异常映射,但它们也被忽略。

您知道要添加/删除/更改哪些内容才能使其正常工作吗?这不是深奥的东西,这是基本的:|

最佳答案

异常映射功能的主要候选者是引发异常的操作。

Docs :

Exception mappings is a powerful feature for dealing with an Action class that throws an Exception. The core idea is that an Exception thrown during the Action method can be automatically caught and mapped to a predefined Result.

但是从拦截器抛出的异常也可以由exception拦截器处理。为了捕获其他拦截器异常,异常拦截器应定义为堆栈中的第一个拦截器。

来自ExceptionMappingInterceptor javadoc :

It is recommended that you make this interceptor the first interceptor on the stack, ensuring that it has full access to catch any exception, even those caused by other interceptors.

关于exception - 为什么从我的拦截器引发的异常没有被 <global-exception-mappings> 捕获?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27622750/

相关文章:

mongodb - Mongodb 排序异常 - 没有索引的 sort() 数据太多

c++ - 使用 STL 字符串和 vector 类的异常问题

error-handling - Struts2 错误处理 - 未找到异常堆栈

java - Struts2 + Spring + JPA( hibernate ): action mapping problem

java - 在 Spring Web Flux 中捕获异常

java - struts2中如何获取xml作为响应

java - 附加到某些 javascript 文件的非法字符

java - Struts "org.apache.struts.action.RequestProcessor - Unhandled Exception thrown"- 如何获取完整的堆栈跟踪?

Java异常处理无效输入