简体   繁体   中英

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:

@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. 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.

You can use <global-exception-mappings> in struts.xml. Global exception mappings are per S2 package, so you can define different mappings for actions by putting them into separate packages.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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