This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Struts and the fly likes Exceptional Handling in Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Exceptional Handling in Struts" Watch "Exceptional Handling in Struts" New topic

Exceptional Handling in Struts

Ganesh Kumar
Ranch Hand

Joined: Jul 02, 2007
Posts: 113

can you explain how to handle exceptions in struts.

sathish kumar
Ranch Hand

Joined: Feb 14, 2007
Posts: 47
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

Joined: May 18, 2012
Posts: 1

<%@taglib uri="" prefix="html"%>
<%@taglib uri="" prefix="bean"%>

<title><bean:message key="label.title" /></title>
<h3><bean:message key="label.header" /></h3>
<html:form action="/exceptionExAction">
<bean:message key="" /> :
<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>


"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"" >

<display-name>Maven Struts Examples</display-name>





<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"


<form-bean name="myDynaForm"
<form-property name="name" type="java.lang.String"/>

path="/failed.jsp" />





parameter="com.myapp.struts.ApplicationResources" />


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{
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);


package com.myapp.struts;


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();

throw new IOException();


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


<%@taglib uri="" prefix="html"%>
<%@taglib uri="" prefix="logic"%>
<%@taglib uri="" prefix="bean"%>

<%@page import="org.apache.struts.Globals"%>
<title><bean:message key="label.title" /></title>
<h3><bean:message key="label.header" /></h3>
<div style="color: red;">
I agree. Here's the link:
subject: Exceptional Handling in Struts
It's not a secret anymore!