DispatchAction Example in Struts 1.3

DispatchAction Example in Struts 1.3

Previous Home Next

 

DispatchAction Example in Struts 1.3 the following tool are required for run this example
  • JDK 1.5
  • MyEclipse IDE
  • Server Tomcat 6.0
  • Struts 1.3 jar file

Directory Structure of DispatcherActionExample 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><bean:message key="label.title" /></title>
</head>
<body>
<h3><bean:message key="label.header" /></h3>
<ul>
<li><html:link page="/dispatcherAction.do?method=add">Add Number</html:link></li>
<li><html:link page="/dispatcherAction.do?method=multiply">Multiply Number</html:link></li>
<li><html:link page="/dispatcherAction.do?method=subtract">Subtract Number</html:link></li>
</ul>
</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/>
	<action-mappings>	
		<action
			path="/dispatcher"
			type="org.apache.struts.actions.ForwardAction"
			parameter="/index.jsp"/>		
		<action
			path="/dispatcherAction"
			type="org.r4r.struts.MyDispatcherAction"
			parameter="method"
			>
			<forward name="add" path="/add.jsp"/>
			<forward name="multiply" path="/multiply.jsp"/>
			<forward name="subtract" path="/subtract.jsp"/>
		</action>		
	</action-mappings>
	<message-resources
		parameter="org.r4r.struts.ApplicationResources" />
</struts-config>

MyDispatcherAction.java

package org.r4r.struts;

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.actions.DispatchAction;

public class MyDispatcherAction extends DispatchAction {
	public ActionForward add(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) 
	throws Exception {		
		return mapping.findForward("add");
	}
	public ActionForward multiply(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) 
	throws Exception {		
		return mapping.findForward("multiply");
	}
	public ActionForward subtract(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) 
	throws Exception {		
		return mapping.findForward("subtract");
	}
}

ApplicationResources.properties

# Resources for parameter 'org.r4r.struts.ApplicationResources'
# Project Struts1.3_DispatcherActionExample
label.title = Struts 1.3 DispatcherAction example
label.header = Struts 1.3 DispatcherAction example

add.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><bean:message key="label.title" /></title>
</head>
<body>
<h3><bean:message key="label.header" /></h3>
<h4>This message is generated by <span style="color: red;">
ADD METHOD</span> using dispatcher action in struts 1.3</h4>
</body>
</html>

multiply.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><bean:message key="label.title" /></title>
</head>
<body>
<h3><bean:message key="label.header" /></h3>
<h4>This message is generated by <span style="color: red;">
MULTIPLY METHOD</span>using dispatcher action in struts 1.3</h4>
</body>
</html>

multiply.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><bean:message key="label.title" /></title>
</head>
<body>
<h3><bean:message key="label.header" /></h3>
<h4>This message is generated by <span style="color: red;">
SUBTRACT METHOD</span> using dispatcher action in struts 1.3</h4>
</body>
</html>

Output




Previous Home Next