简体   繁体   English

Struts 2为某些操作添加异常映射

[英]Struts 2 add exception mapping for certain actions

Consider a project with lots of annotated actions. 考虑一个带有大量注释动作的项目。

public class TransferMoney(){

    @Action("transfer-money-show-form")
    public String showForm();

    @Action("transfer-money-confirm")
    public String confirmForm();

    @Action("transfer-money-result")
    public String result();
}

I want to add exception-mapping to confirmForm so I can do it as: 我想添加异常映射到confirmForm所以我可以这样做:

@Action(value = "transfer-money-confirm", 
        exceptionMappings = 
                 {@ExceptionMapping(
                       exception = "java.lang.Exception", 
                        result = "exception")
                  }
        )

However is it a better way ?! 但是,这是更好的方法吗? As I said I have lots of actions and I don't want to add exceptionMapping for each of them one by one. 正如我所说的,我有很多动作,我不想为每个动作一个一个地添加exceptionMapping The action name which I want to add mapping to them all ends with confirm but it seems not useful because the Exception Mapping does not accept regex. 我想要向其添加映射的操作名称都以confirm结尾,但是它似乎没有用,因为Exception Mapping不接受正则表达式。

You can use <global-exception-mappings> in struts.xml. 您可以在struts.xml中使用<global-exception-mappings> Global exception mappings are per S2 package, so you can define different mappings for actions by putting them into separate packages. 全局异常映射是针对每个S2包的,因此您可以将动作放入单独的包中,从而为操作定义不同的映射。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM