Exceptions are common in any application. We try to catch it and process it but sometimes our application is not able to process it and it’s thrown to the container. Struts2 provides a robust mechanism through which we can provide customized response to client whenever any exception is thrown by the application.

We know that Struts2 interceptors are like servlet filters that provide pre-processing of request and post-processing of 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. Let’s try to understand it’s usage with a simple application.

We will create a dynamic web project and then configure it as Maven project. Our project structure looks like below image.


Web Application Configuration for Struts2

Add struts2 dependency in pom.xml file like below.

Struts2 filter configuration in deployment descriptor file.


Action Classes

We will create few action classes whose only purpose will be to throw some exceptions.

Struts2 Configuration

We are configuring package level exception handling and action level exception handling in struts.xml configuration file.


Notice that we need to define global result pages for global level exception mapping to result pages. For action level exception handling, we need to define result page in action mapping.

Since there is no exception result pages mapped for MyAction and it’s throwing NullPointerException, the closest base class with exception mapping is RuntimeException, so it should return runtime_exception.jsp page.

For MySpecialAction throwing IOException, the result page is defined and it should return error.jsp page.

Let’s write these JSP pages and confirm this behavior.

JSP Pages



Notice that we have defined exception.jsp as result page for any java.lang.Exception but since there was no exception that is the closest match to this, it’s not used.

Run the Application

Now when we run the application, we get following pages as response confirming the exception handling behavior.



That’s all for Exception Handling in Struts2 applications, you can download the project from below link and play around with it for more learning.

By admin

Leave a Reply

%d bloggers like this: