Introduction of Struts 2 Exceptions

We overcome with so many exceptions in the struts 2 application exceptions are so common and obvious in any application. We need to follow the proper mechanism for the exception handling in our application to make it efficient and bug free. We try to catch it and process it but sometimes our application is not able to process it and itís thrown to the web container. Struts2 provides a robust mechanism which is very useful to work with the exceptions through which we can provide customized response to client whenever any exception is thrown by the application.

We know that in Apache Struts2 interceptors are like servlet filters that provide pre-processing of request and post-processing of response or an application response. Struts2 provides exception handling support through com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor interceptor. This interceptor is part of basicStack and defaultStack interceptors stack, so we donít need to configure them for our application usage.

We can provide exception handling at package level through global-exception-mappings and global-results and we can use exception-mapping in action mappings for action level exception handling. We need to provide result pages to use for different exception scenarios. We can try some examples to understand itís usage with a simple application.

There are two methods for handing uncaught exceptions in Struts2:

    • Global exception handling: specifies exception mappings (exception type - view name) which apply to all action classes in a Struts2 package.

    • Exception handling per action: specifies exception mappings which apply to a specific action class.

Both methods require adding exception mappings in struts.xml configuration file.