Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptional Handling in Struts

 
Ganesh Kumar
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


can you explain how to handle exceptions in struts.


Thankyou
Ganeshkumar
 
sathish kumar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default till struts version 1.1 there, struts did not have much of exception handling, developers had to work on the code to do effective exception handling.
But later versions of struts have good exception handling techniques.
Depending upon whether it is runtime or compile time exception struts can handle thing.
For example in your code, say user when not able to log-on successfully, then it has to be redirected to a graceful error page with an exception.
In this case you can use the "<exception>" in "struts.config".
Another one that will help a lot is ModuleException api, I think it extends exception and action errror class,Classic api that can be used for various scenarios.
As well ExceptionHandler is another handy.
If you want to display error messages in your JSP, I would take a look at ActionErrors and ActionMessages Api.
Hope it helps.
 
surendra sukavasi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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="com.myapp.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="com.myapp.struts.MyExceptionAction"
name="myDynaForm"
>

</action>
</action-mappings>

<message-resources
parameter="com.myapp.struts.ApplicationResources" />

</struts-config>

MyExceptionHandler.java

package com.myapp.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 com.myapp.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>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic