Search code examples
javajsperror-handlingstruts2xml-configuration

Global exception handling in Struts 2


How can I implement a global exception handler for Apache Struts 2?

I'm using annotations (with struts.property) and NOT struts.xml.

I have got it to work for action-classes (at class and method level) but I would like something more global like a stand-alone class.

Can I for example use the ExeptionHandler from Struts 2? Hard to find documentation and examples.

Any ideas?


Solution

  • You can map exceptions to result that calls the action that would handle the exception.

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

    References: