Struts 2 Exception Handling with @ExceptionMapping Annotation
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"}) })
@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"}) })
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."); } }
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>
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>