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>