Pages Navigation Menu

Coding is much easier than you think

Exception handling in Struts 2

Exception handling in Struts 2

 

dwd2
Zip file – Struts2Exception.zip

 
The Struts 2 framework provides some easy way to handle any uncaught exceptions. It works on exception interceptor which is part of default-stack in struts-default.xml file.
 
There are two ways to handle uncaught exceptions in Struts2:

  • Global exception handling: specifies exception mappings which apply to all action classes in a Struts2.
  • 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.
 
** UPDATE: Struts 2 Complete tutorial now available here.
 

Global Exception Handling :

 

Add the following code snippet just after the <package> element in struts.xml file:


  /jsp/NullPointer.jsp
  /jsp/GlobalExceptioon.jsp



  
  

 
<global-exception-mappings> – specifies a set of element which maps an exception type to a view name.
<global-results> – element defines global view names and its mapping page.
 
Here in global-exception-mapping the exception of type java.lang.NullPointerException is mapped to the view name “null”. So when any uncaught exception of type java.lang.NullPointerException or its sub types is thrown, Struts will redirect users to the view page mapped with the name “null” and in the view named “null” is mapped with the view page “jsp/NullPointer.jsp”.
 

Exception Handling Per Action

 
If you need to handle an exception in a specific way for a certain action you can use the exception-mapping node within the action node.

// Here an action configuration adds an addition exception handler. 


/jsp/Success.jsp
/jsp/Error.jsp

 
Here If NumberFormatException occurs in Addition action then Error.jsp will be executed else framework searches in the global exception list and execute the respective page using global-result type.
 
In the Error page we can access information of the exception as follows:
1. Exception name: <s:property value=”exception”/>
2. Exception stack trace: <s:property value=”exceptionStack”/>
3. Exception in the value stack: <s:property value=”[0].top” />

 

Implementation – exception handling in Struts 2.

 
File: Struts.xml
 




  /jsp/NullPointer.jsp
  /jsp/GlobalExceptioon.jsp


// Here the results are expected to be global results. 

  
  


// Here an action configuration adds an addition exception handler.

  
     /jsp/Success.jsp
     /jsp/Error.jsp



     /jsp/Success1.jsp



     /jsp/Success1.jsp



 

Action class

 
File: Addition.java
For Exception Handling in Action class – here if both numbers is not parsed in int, it will throw NumberFormatException.

package simplecode.action;

import com.opensymphony.xwork2.Action;

public class Addition implements Action 
{
	private String number1, number2;
	private String sum = "";

	public String getNumber1() {
		return number1;
	}

	public String getNumber2() {
		return number2;
	}

	public void setNumber1(String number1) {
		this.number1 = number1;
	}

	public void setNumber2(String number2) {
		this.number2 = number2;
	}

	public String getSum() {
		return sum;
	}

	public void setSum(String sum) {
		this.sum = sum;
	}

	public String execute() throws Exception {
		int num1 = Integer.parseInt(number1);
		int num2 = Integer.parseInt(number2);
		int sum = num1 + num2;
		setSum("" + sum);
		return SUCCESS;
	}
}

 
File: Action1.java – throw NullPointerException

package simplecode.action;

import com.opensymphony.xwork2.Action;

public class Action1 implements Action 
{
	public String execute() throws Exception 
	{
		String string = null;
		System.out.println(string.charAt(7));
		return SUCCESS;
	}
}

File: Action2.java – throw NumberFormatException.

package simplecode.action;

import com.opensymphony.xwork2.Action;

public class Action2 implements Action 
{
	public String execute() throws Exception {
		int num = Integer.parseInt("7b");
		return SUCCESS;
	}
}

 

Jsp Pages

 

File: index.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>



   
   
   


Null Pointer Exception example - ">ActionLink1

Global Exception - ">ActionLink2

 
File : Error.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>


Unexpected error occurred (Exception per Action page)

Please contact your system administrator

Error Message


Exception object transferred to value stack

Stack Trace

 
File: NullPointer.jsp

<%@taglib uri="/struts-tags" prefix="s"%>


Unexpected error occurred (Global - Null Pointer exception)

Please contact your system administrator

Error Message

Exception object transferred to value stack

 
File: GlobalException.jsp

<%@taglib uri="/struts-tags" prefix="s"%>


Global - NumberFormatException


Global NumberFormatException occurred

Please contact your system administrator

Error Message


Exception object transferred to value stack

 

Demo

 
http://localhost:8089/Struts2Exception/
 
Struts 2 exception
 
On giving the input as above the result will be as shown.
 
With exception
 
Global exception- On clicking ActionLink1 and ActionLink2 the following global exception page will be rendered.
 
Global exception in struts2
 
Global exception 2 in struts2
 

Leave a Comment

Your email address will not be published. Required fields are marked *