Struts 2 Exception Handling with @ExceptionMapping Annotation

Written by: Arvind Rai,
Last updated:
June 12, 2014
On 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.
@ExceptionMappings({
    @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.

Action Class for Exception Handling in Struts 2

Find the action class that is annotated with @ExceptionMappings at class level.
UserAction.java
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;
@Namespace("/user")
@Results({
	  @Result(name="success", location="/user/user.jsp"),
})
@ExceptionMappings({
    @ExceptionMapping(exception = "java.lang.NullPointerException", result = "success")
})
public class UserAction extends ActionSupport{
	@Action("/demo")
	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.
user.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Struts 2 Annotation Example</title>
</head>
<body>
<h1>Struts 2 Annotation Example</h1>
<s:property value="exception"/>
<!--s:property value="exceptionStack"/-->
</body>
</html>
 
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.
web.xml
<web-app>
  <display-name>Struts 2  Annotation Example</display-name>
   <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>actionPackages</param-name>
            <param-value>com.concretepage.action</param-value>
        </init-param>
    </filter>
  <filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
 

pom.xml for Struts 2

Find the maven dependencies to run the struts application.
<dependencies>
	<dependency>
		<groupId>org.apache.struts</groupId>
		<artifactId>struts2-core</artifactId>
		<version>2.3.16</version>
	</dependency>
	<dependency>
		<groupId>org.apache.struts</groupId>
		<artifactId>struts2-convention-plugin</artifactId>
		<version>2.3.8</version>
	</dependency>
</dependencies>
 

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

Join the Newsletter

(Subscribe to get our latest content directly into your inbox)

WRITTEN BY
ARVIND RAI
ARVIND RAI









©2024 concretepage.com | Privacy Policy | Contact Us