Exception Handling with Struts

Exception Handling with Struts

Previous Home Next

 

When any exception occurs then we have to handle this exception and show the proper massage to client side (on web browser of user). Struts provides tag to show exception on JSP View.


 
Struts provides effective exception-handling framework.
There are two way for the exception handling in struts:-

1. The First approach the exceptions control by try/catch block and display some meaningful messages are displayed. The developer has to write the code for the flow of the application.

2. The second approach the exceptions are defined in the struts-config file.if any exception occurs then control is automatically passed to the appropriate error page. 

The <exception> tag have following attributes :-

1. Key
2. Type
3. Path
4. Handler

There are following two sections in struts-config.xml where you can define the exceptions.

1. With Any Action mapping:- The action class is identified which may throw the exception like password failure, resource access etc.

Example :-

struts-config.xml

<action path="/exceptionexample"parameter="method" 
input="/index.jsp" type="r4r.co.in.ExceptionExampleAction"  >
<exception key="ABC.key" type="java.lang.RuntimeException" path="/index.jsp" />       
</action>

 2. We can define the exception in struts-config.xml :- If any type of exception occurs the control is passed globally to the exception and control is passed to the error page.

When an Action's class execute a method that throws an exception, then an ExceptionHandler is define to handle the execute. Class ExceptionHandler is define in 
the package of org.apache.struts.action.ExceptionHandler and override the execute method. Now the execute method should proceed the Exception and
return an ActionForward object, which configure your handler in struts-config.xml like this-

 Example:-

struts-config.xml

<global-exceptions>   
<exception key="ABC.key" type="java.lang.RuntimeException" path="/index.jsp" />
</global-exceptions>

Above configuration element says that r4r.co.in.ExceptionHandler.execute will be called when any Exception is thrown by an Action. and the key is a key into your message resources properties file that can be used to retrieve an error message. If the handler attribute is not specified so, the default handler stores the exception in the request attribute under the value of the Globals.EXCEPTION_KEY global key.

A common use of ExceptionHandlers is to configure one for java.lang.Exception so it's called for any exception and log the exception to some data store. 


Directory Structure of ExceptionHandlingExample in Struts 1.3 Using MyEclipse IDE




index.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
 
<html>
<head>
<title><bean:message key="label.title" /></title>
</head>
<body>
<h3><bean:message key="label.header" /></h3>
<html:form action="/exceptionExAction">
<bean:message key="label.name" /> :
<html:text property="name" size="20" maxlength="20"/> <br/><br/>
<html:submit><bean:message key="label.submit" /></html:submit>
<html:reset><bean:message key="label.reset" /></html:reset>
</html:form> 
</body>
</html>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<web-app>
  <display-name>Maven Struts Examples</display-name>
 
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>
        org.apache.struts.action.ActionServlet
    </servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>
         /WEB-INF/struts-config.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
 
  <servlet-mapping>
       <servlet-name>action</servlet-name>
       <url-pattern>*.do</url-pattern>
  </servlet-mapping>
 
</web-app>

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
 
<struts-config>
 
	<form-beans>
		<form-bean name="myDynaForm"   
		      type="org.apache.struts.action.DynaActionForm">
		      <form-property name="name" type="java.lang.String"/>
		</form-bean>
	</form-beans>
 
    <global-exceptions>
	    <exception
	      key="error.exception.message"
	      type="java.io.IOException"
	      handler="org.r4r.struts.MyExceptionHandler"
	      path="/failed.jsp" />
	</global-exceptions>
	
	<action-mappings>

	    <action
			path="/exceptionEx"
			type="org.apache.struts.actions.ForwardAction"
			parameter="/index.jsp"/>
 
		<action
			path="/exceptionExAction"
			type="org.r4r.struts.MyExceptionAction"
			name="myDynaForm"
			>
			
		</action>
	</action-mappings>
 
	<message-resources
		parameter="org.r4r.struts.ApplicationResources" />
 
</struts-config>

MyExceptionHandler.java

package org.r4r.struts;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;
 
public class MyExceptionHandler extends ExceptionHandler{
	@Override
	public ActionForward execute(Exception ex, ExceptionConfig ae,
			ActionMapping mapping, ActionForm formInstance,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException {
		
		return super.execute(ex, ae, mapping, formInstance, request, response);
	}
 
}

MyExceptionAction.java

package org.r4r.struts;
 
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;
 
public class MyExceptionAction extends Action{
 
	public ActionForward execute(ActionMapping mapping,ActionForm form,
		HttpServletRequest request,HttpServletResponse response) 
        throws Exception {
 
		ActionMessages errors = new ActionMessages();
	    saveErrors(request,errors);
	    
	    throw new IOException();
	}
 
}

ApplicationResources.properties

#common module error message
error.exception.message = This Exception Message Generated By Struts 1.3 Exception Handler Example
label.name = User Name
label.submit = Submit
label.reset = Reset
error.name.required = User Name Resquired
label.title = Struts 1.3 Exception Handler Example
label.header = Struts 1.3 Exception handler Example

failed.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>

<%@page import="org.apache.struts.Globals"%>
<html>
<head>
<title><bean:message key="label.title" /></title>
</head>
<body>
<h3><bean:message key="label.header" /></h3>
<div style="color: red;">
<html:errors/>
</div>
</body>
</html>

Output




Previous Home Next