Home  >  Struts 2

Struts 2 Exception Handling Example with @ExceptionMapping Annotation

By Arvind Rai, June 12, 2014
In this page we will learn struts 2 annotation based exception handling. @ExceptionMappings and @ExceptionMapping are two annotations to handle exception in struts 2. Find the syntax below.
    @ExceptionMapping(exception = "java.lang.NullPointerException", result = "success", 
	                                    params = {"param1", "val1"})
The above annotation can be used at class level and it will be applied for all action defined in action class.

@ExceptionMappings : This is group of @ExceptionMapping
@ExceptionMapping : This defines the exception mapping
exception attribute: This is the attribute which declares the exception class.
result attribute: This plays the role to get result so that appropriate page could be redirected.
params attribute: The value defined by param is passed to result.

If we want to handle exception action wise, we can use annotation at method level as below.
@Action(value = "exception1", exceptionMappings = {
            @ExceptionMapping(exception = "java.lang.NullPointerException", result = "success", 
			params = {"param1", "val1"})
In our example I am using @ExceptionMapping at class level.

Software Used

To develop and run the demo, we need below softwares.
1. Eclipse
2. JDK 6
3. Tomcat 7

Action Class for Exception Handling in Struts 2

Find the action class that is annotated with @ExceptionMappings at class level.
package com.concretepage.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.ActionSupport;
	  @Result(name="success", location="/user/user.jsp"),
    @ExceptionMapping(exception = "java.lang.NullPointerException", result = "success")
public class UserAction extends ActionSupport{
	public String demo() {
	    throw new NullPointerException("Exception Occured.");
There is one method in action class that is throwing NullPointerException. result attribute of @ExceptionMappings transfers the control to jsp related to SUCCESS result.

JSP page: Use of exception and exceptionStack

Find the jsp which displays the result.
<%@ taglib prefix="s" uri="/struts-tags" %>
<head><title>Struts 2 Annotation Example</title>
<h1>Struts 2 Annotation Example</h1>
<s:property value="exception"/>
<!--s:property value="exceptionStack"/-->
There are two attribute with which error is displayed on view page.
exception : displays short description of error.
exceptionStack : displays the stack trace of error.

web.xml Configuration for Struts 2

Find the web.xml.
  <display-name>Struts 2  Annotation Example</display-name>

pom.xml for Struts 2

Find the maven dependencies to run the struts application.

Eclipse Configuration of files for Exception Handling in Struts 2

Find the screen shot of eclipse configuration.
Struts 2 Exception Handling Example with @ExceptionMapping Annotation

Exception Handling Demo Output

To run the application use the URL http://localhost:8080/Struts2Demo-1/user/demo and output will look like as below.
Struts 2 Exception Handling Example with @ExceptionMapping Annotation

Download Source Code


©2019 concretepage.com | Privacy Policy | Contact Us