【问题标题】:Global exception handling in Struts 2Struts 2 中的全局异常处理
【发布时间】:2016-03-08 14:04:13
【问题描述】:

如何为 Apache Struts 2 实现全局异常处理程序?

我正在使用注释(使用 struts.property)而不是 struts.xml。

我已经让它适用于动作类(在类和方法级别),但我想要更全球化的东西,比如独立类。

例如,我可以使用 Struts 2 中的ExeptionHandler 吗?很难找到文档和示例。

有什么想法吗?

【问题讨论】:

    标签: java jsp error-handling struts2 xml-configuration


    【解决方案1】:

    您可以将异常映射到调用处理异常的操作的结果。

    <global-results>
        <result name="exception" type="chain">
            <param name="actionName">exceptionHandler</param>
            <param name="namespace">/</param>
        </result>
    </global-results>
    <global-exception-mappings>
        <exception-mapping exception="java.lang.Exception" result="exception" />
    </global-exception-mappings>
    

    参考资料:

    【讨论】:

    • 但是不应该放在struts.xml里面吗?我正在处理的项目不使用 struts.xml(不存在),它使用注释。
    • @Staplerz 注释是与 Struts2 集成的附加功能,它不会替换 xml 配置。
    • 哦,我明白了,我是 struts2 的新手...我发现 struts2 中有一个 ExceptionHandler 类,可以在 struts.xml 文件中定义为 根据以下示例的元素:mkyong.com/struts/struts-global-custom-exception-example 这更像我正在寻找的东西。但是我无法让它工作。有什么想法吗?
    • Struts2 中没有ExceptionHandler 类,你可以使用exception 拦截器和全局异常映射。该示例适用于 Struts1.3.x,它是一个与 Struts2 不同的框架。
    • 哦,好像是struts 1的一部分,我试试你的解决方案。谢谢!
    猜你喜欢
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 2019-07-18
    • 2017-02-24
    • 2011-05-20
    • 1970-01-01
    相关资源
    最近更新 更多