Search code examples
jspservletsstruts2filterinterceptor

How to handle HTTP Errors in Struts2


I am working on a Struts2 project which encounter HTTP errors like 404: page not found or 500: Internal server Error. I just want to handle all HTTP errors in Struts 2 programmatically without configuration.

Perhaps I would detect HTTP error code in filter or interceptor and then forward request to another page which especially design to display proper messages. How can I design such a program?


Solution

  • Take a look at the Struts2 Exception Handling Wiki.

    You can define global results for specific Exceptions.

    <global-results>
     <result name="securityerror">/securityerror.jsp</result>
     <result name="error">/error.jsp</result>
    </global-results>
    
    <global-exception-mappings>
     <exception-mapping exception="org.apache.struts.register.exceptions.SecurityBreachException" result="securityerror" />
      <exception-mapping exception="java.lang.Exception" result="error" />
    </global-exception-mappings>