Friday, May 7, 2010

Struts Application- LookUpDispatchAction

Struts Application- LookUpDispatchAction


Purpose:

Org.apache.struts.actions.LookUpDispatchAction is a built in action in struts framework.
This class enables a user to perform different task using a single action class. It eliminate for creating multiple action class for each task.

DispatchAction and LookupDispatchAction are same But LookupDispatchAction uses java Map interface and ApplicationResources.properties to dispatch method.


Procedure:

Step_1:

Creating action class.

                             When you create action class you select configuration file into

                              org.apache.struts.actions.LookupDispatchAction

               

package actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.LookupDispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import java.util.*;

public class lookAction extends LookupDispatchAction
{


protected Map getKeyMethodMap()
{
Map map = new HashMap();
map.put("suresh.one", "one");
map.put("suresh.two", "two");
map.put("suresh.three", "three");
return map;
}

public ActionForward one(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws java.lang.Exception
{

return mapping.findForward("one");
}


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

return mapping.findForward("two");
}


public ActionForward three(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws java.lang.Exception
{

return mapping.findForward("three");
}


}


Step_2:

Creating actionform class.

You create action form class . where you insert setter and getter method for method;

Private String method;



package actionforms;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;


public class lookForm extends org.apache.struts.action.ActionForm
{

private String method;

public String getMethod() {
return method;
}

public void setMethod(String method) {
this.method = method;
}
}

Step_3:

Made some change in ApplicationResources.properties.

Add below code.

suresh.one=one
suresh.two=two
suresh.three=three

Step_4:

Made change in Struts-congif.xml

< action-mappings >
< action
input="/"
name="lookForm"
path="/look"
scope="session"
validate="false"
parameter="method"
type="actions.lookAction" >
< forward name="one" path="/jsp/one.jsp"/ >
< forward name="two" path="/jsp/two.jsp"/ >
< forward name="three" path="/jsp/three.jsp"/ >
< /action>

< /action-mappings >


Step_5:

Creating jsp file for look.jsp


< %@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< %@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >
< %@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >

< html >

< head > < title > JSP Page < /title>
< /head >
< body >
< html:form action="/look.do?method=one" >
< html:submit value="one"/ >
< /html:form >
< html:form action="/look.do?method=two" >
< html:submit value="two"/ >
< /html:form >
< html:form action="/look.do?method=three" >

< html:submit value="three"/ >

< /html:form >

< /body >

< /html >


Step_6:

Create one.jsp and two.jsp and three.jsp.


Step_7:

Run look.jsp

Right click on look.jsp and run the file. Or Shift+f6.


Note: You have any doubt. Give comments. You give your email address in comments. I shall workspace for the struts project to your email address.

No comments:

Post a Comment