WalkinsForum-Jobs,Job Openings,Daily Walkins,Weekend Walkins,Vacancies,Off campus,Interviews Forum
Struts Dispatch Action Tutorial with example - Printable Version

+- WalkinsForum-Jobs,Job Openings,Daily Walkins,Weekend Walkins,Vacancies,Off campus,Interviews Forum (http://www.walkinsforum.com)
+-- Forum: Programming Forum (/Forum-programming-forum)
+--- Forum: Java (/Forum-java)
+---- Forum: struts tutorial (/Forum-struts-tutorial)
+---- Thread: Struts Dispatch Action Tutorial with example (/Thread-struts-dispatch-action-tutorial-with-example)



Struts Dispatch Action Tutorial with example - rajasri - 12-12-2011 08:56 PM

Today we are going to learn

Struts Dispatch Action Tutorial with example

The two package which needed to be imported are as follows
import org.apache.struts.action.DispatchAction;
import org.apache.struts.action.ActionForm;

The Main features of Dispatch Action are

*One of the Built in Functions in Struts Framework
*The class doesnot provide implement action for the execute() method,Because dispatch action class iteself
implements the method.
*It eliminates the need of creating multiple independent actions
Lets create a jsp page which calls the dispatch action classes in struts-config.xml

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

Quote:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<b><font color="Green">Tutorial Pruodly Presented by <a href="http://s2sgateway.com">http://s2sgateway.com</a></font></b><br>
<html:link page="/dispatch.jsp">dispatch</html:link>
<html:link page="/dispatch.do?parameter=tutorial">Tutorials</html:link>
<html:link page="/dispatch.do?parameter=downloads">Downloads</html:link>
</body>
</html>

In dispatch action.java file we can redirect the form using different functions, so we don't need to use default execute() method
[java lines="50"]

package com.s2sgateway.action;
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;
import com.s2sgateway.form.DispatchForm;
public class DispatchAction1 extends DispatchAction {

public ActionForward tutorial(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {

return mapping.findForward("tutorial");
}
public ActionForward downloads(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {

return mapping.findForward("downloads");
}
}
In struts-config.xml we can define the forward name and path to be redirected

Quote:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="dispatchForm" type="com.s2sgateway.form.DispatchForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="dispatchForm"
input="/dispatch.jsp"
name="dispatchForm"
parameter="parameter"
path="/dispatch"
scope="request"
type="com.s2sgateway.action.DispatchAction1">
<forward name="tutorial" path="/tutorial.jsp" />
<forward name="downloads" path="/downloads.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.s2sgateway.ApplicationResources" />
</struts-config>

The other file named dispatch.java is doing a same function as index.jsp but it gets the parameter value in textbox rather than link
dispatchs2sgateway.

Happy coding