Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Struts LookupDispatchAction Example 
 

Struts LookupDispatch Action (org.apache.struts.actions.LookupDispatchAction) is one of the Built-in Actions provided along with the struts framework.

 

Struts LookupDispatchAction Example

                         

Struts LookupDispatch Action (org.apache.struts.actions.LookupDispatchAction) is one of the Built-in Actions provided along with the struts framework.

.The org.apache.struts.actions.LookupDispatchAction class.is a subclass of org.apache.struts.actions.DispatchAction class.This class enables a user to collect related functions into a single action class. It eliminates the need of  creating multiple independent actions for each function. Here in this example you will learn more about Struts LookupDispatchAction that will help you to grasp the concept better.

Let's develop a class  LookupDispatch_Action which is a sub class of org.apache.struts.actions.LookupDispatchAction class. This class does not provide an implementation for the execute() method because DispatchAction class itself implements this method.  LookupDispatchAction  class is much like the DispatchAction class except that it uses a Java Map  and ApplicationResource.properties file to dispatch methods . At run time, this class  manages to  delegate the request to one of the methods of the derived Action class. Selection of a method depends on the value of  the  parameter  passed from the incoming request. LookupDispatchAction uses this parameter value  to reverse-map to a property in the Struts Resource bundle file (ie..ApplicationResource.properties). This eliminates the need of creating an instance of ActionForm  class. 

LookupDispatch_Action class contains multiple methods ie.. add() , edit() , search()  , save() . Here all the methods are taking the same input parameters but each method returns a different ActionForward like "add" in case of add() method , "edit" in case of edit() etc. Each ActionForward is defined in the struts-config.xml file (action mapping is shown later in this page). 

Notice the implementation of the getKeyMethodMap()method.This method is required  to  map the names of the  keys  in the Struts Resource bundle file (ie..ApplicationResource.properties) to the methods in the class. The key values in the bundle file are matched against the value of the incoming request parameter ( which is  specified in the action tag through struts-config.xml file). Then this matching key is mapped to the appropriate  method to execute ,the mecahanism is implemented through the getKeyMethodMap()and can be defined as key-to-method mapping.

Here is the code for Action Class

package roseindia.net;

/**
@author Amit Gupta
* @Web http://www.roseindia.net
* @Email struts@roseindia.net
**/

import java.io.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.actions.LookupDispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LookupDispatch_Action extends LookupDispatchAction
{
  protected Map getKeyMethodMap(){
        Map map =  new HashMap();
    map.put("roseindia.net.add","add");
    map.put("roseindia.net.edit","edit");
    map.put("roseindia.net.search","search");
    map.put("roseindia.net.save","save");
    return map;
    }
  
  
  public ActionForward add(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse responsethrows Exception{
    System.out.println("You are in add function.");
      return mapping.findForward("add");
  }

  public ActionForward edit(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse responsethrows Exception{
    System.out.println("You are in edit function.");
    return mapping.findForward("edit");
  }

    public ActionForward search(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse responsethrows Exception{
    System.out.println("You are in search function");
    return mapping.findForward("search");
  }
    
  public ActionForward save(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse responsethrows Exception{
    System.out.println("You are in save function");
    return mapping.findForward("save");
  }
}

No need to Develop an ActionForm Class 

Instead create an Application Resource Property File:

Application.properties
in the same directory structure where classes are saved.

roseindia.net.add=add
roseindia.net.edit=edit
roseindia.net.search=search
roseindia.net.save=save

Add the following, Message Resources Definitions in  struts-config.xml

<message-resources parameter="roseindia.net.ApplicationResources" />

Develop the following Action Mapping in the struts-config.xml

Here, Action mapping helps to select the method from the Action class for specific requests. Note that the value specified with the parameter 
attribute is used to delegate request to the required method  of the LookupDispatch_Action Class.

<action
path="/LookupDispatchAction"
type="roseindia.net.LookupDispatch_Action"
parameter="parameter"
input="/pages/LookupDispatchAction.jsp"
name="LookupDispatchActionForm"
scope="request"
validate="false">
<forward name="add" path="/pages/LookupDispatchActionAdd.jsp" />
<forward name="edit" path="/pages/LookupDispatchActionEdit.jsp" />
<forward name="search" path="/pages/LookupDispatchActionSearch.jsp"/>
<forward name="save" path="/pages/LookupDispatchActionSave.jsp" />
</action>

Developing jsp page

Code of the jsp (LookupDispatchAction.jsp)  to delegate requests to different jsp pages :

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%> 
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> 
<html:html locale="true">
<HEAD>
<TITLE>Dispatch Action Example</TITLE>
<BODY>

<H3>Dispatch Action Example</H3>
<p><html:link page="/LookupDispatchAction.do?parameter=add">Call Add Section</html:link></p> 
<p><html:link page="/LookupDispatchAction.do?parameter=edit">Call Edit Section</html:link></p> 
<p><html:link page="/LookupDispatchAction.do?parameter=search">Call Search Section</html:link></p> 
<p><html:link page="/LookupDispatchAction.do?parameter=save">Call Save Section</html:link></p> 

</html:html>

Add the following line in the index.jsp to call the form.

<li>
<html:link page="/pages/LookupDispatchAction.jsp">Struts File Upload</html:link>
<br>
Example demonstrates  how LookupDispatchAction class works.
</li>

Building and Testing the Example 

To build and deploy the application go to Struts\Strutstutorial directory and type ant on the command prompt. This will deploy the application. Open the browser and navigate to the LookupDispatchAction.jsp page. Your browser displays the following LookupDispatchAction page.

Selecting  Call Add Section displays the following LookupDispatchActionAdd.jsp page

Selecting Call Edit Section displays the following   LookupDispatchActionEdit.jsp page

Selecting Call Search Section displays the following  LookupDispatchActionSearch.jsp page

Selecting Call Save Section  displays the following  LookupDispatchActionSave.jsp  page

 

                         

» View all related tutorials
Related Tags: c string com jsp forms session url orm data form application io include scope make output type stream request page

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

6 comments so far (
post your own) View All Comments Latest 10 Comments:

<html:link page="/LookupDispatchAction.do?parameter=add">Call Add Section</html:link>

This does not display as a link in my browser at all.it is converted into plain text.

Posted by Anand Kiran on Thursday, 11.15.07 @ 17:04pm | #37494

Hi
in the above example they specified that no need to create Actionform Class, but it is not working when actionForm class is not created.

Posted by sujith on Tuesday, 11.13.07 @ 22:58pm | #37277

interested but a bit confused with mapping in the struts-cofig.xml.

would you please send me an explanation on that.

thanx

Posted by pontsho on Sunday, 10.7.07 @ 12:53pm | #32311

I have tried the same code as above. but i am getting the following exception. Can anybody help me????????????
javax.servlet.ServletException: Action[/LookupDispatchAction] missing resource 'add' in key method map
at org.apache.struts.actions.LookupDispatchAction.getLookupMapName(Ljavax.servlet.http.HttpServletRequest;Ljava.lang.String;Lorg.apache.struts.action.ActionMapping;)Ljava.lang.String;(LookupDispatchAction.java:240)
at org.apache.struts.actions.LookupDispatchAction.getMethodName(Lorg.apache.struts.action.ActionMapping;Lorg.apache.struts.action.ActionForm;Ljavax.servlet.http.HttpServletRequest;Ljavax.servlet.http.HttpServletResponse;Ljava.lang.String;)Ljava.lang.String;(LookupDispatchAction.java:281)
at org.apache.struts.actions.LookupDispatchAction.execute(Lorg.apache.struts.action.ActionMapping;Lorg.apache.struts.action.ActionForm;Ljavax.servlet.http.HttpServletRequest;Ljavax.servlet.http.HttpServletResponse;)Lorg.apache.struts.action.ActionForward;(LookupDispatchAction.java:158)
at org.apache.struts.action.RequestProcessor.processActionPerform(Ljavax.servlet.http.HttpServletRequest;Ljavax.servlet.http.HttpServletResponse;Lorg.apache.struts.action.Action;Lorg.apache.struts.action.ActionForm;Lorg.apache.struts.action.ActionMapping;)Lorg.apache.struts.action.ActionForward;(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(Ljavax.servlet.http.HttpServletRequest;Ljavax.servlet.http.HttpServletResponse;)V(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(Ljavax.servlet.http.HttpServletRequest;Ljavax.servlet.http.HttpServletResponse;)V(ActionServlet.java:1192)
at org.apache.struts.action.ActionServlet.doGet(Ljavax.servlet.http.HttpServletRequest;Ljavax.servlet.http.HttpServletResponse;)V(ActionServlet.java:412)
at javax.servlet.http.HttpServlet.service(Ljavax.servlet.http.HttpServletRequest;Ljavax.servlet.http.HttpServletResponse;)V(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(Ljavax.servlet.ServletRequest;Ljavax.servlet.ServletResponse;)V(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run()Ljava.lang.Object;(ServletStubImpl.java:971)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(Ljavax.servlet.ServletRequest;Ljavax.servlet.ServletResponse;Lweblogic.servlet.internal.FilterChainImpl;)V(ServletStubImpl.java:402)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(Ljavax.servlet.ServletRequest;Ljavax.servlet.ServletResponse;)V(ServletStubImpl.java:305)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run()Ljava.lang.Object;(WebAppServletContext.java:6350)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(Lweblogic.security.subject.AbstractSubject;Ljava.security.PrivilegedAction;)Ljava.lang.Object;(AuthenticatedSubject.java:317)
at weblogic.security.service.SecurityManager.runAs(Lweblogic.security.acl.internal.AuthenticatedSubject;Lweblogic.security.acl.internal.AuthenticatedSubject;Ljava.security.PrivilegedAction;)Ljava.lang.Object;(SecurityManager.java:118)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(Lweblogic.servlet.internal.ServletRequestImpl;Lweblogic.servlet.internal.ServletResponseImpl;)V(WebAppServletContext.java:3635)
at weblogic.servlet.internal.ServletRequestImpl.execute(Lweblogic.kernel.ExecuteThread;)V(ServletRequestImpl.java:2585)
at weblogic.kernel.ExecuteThread.execute(Lweblogic.kernel.ExecuteRequest;)V(ExecuteThread.java:197)
at weblogic.kernel.ExecuteThread.run()V(ExecuteThread.java:170)
at java.lang.Thread.startThreadFromVM(Ljava.lang.Thread;)V(Unknown Source)

Posted by Venkatesh on Friday, 09.28.07 @ 08:57am | #30119

its very good exapmle, everyone understand easily

Posted by rajender on Wednesday, 09.19.07 @ 15:33pm | #27841

I have implemented the code EXACTLY as presented here.
When I click any of the Call Add/Edit/Search/Save Section links the app doesn't forward to the jsp page.

What may be the problem here?
I have been following this tutorial from the beginning and this is the first problem I have come up against.

Posted by Du on Friday, 05.25.07 @ 17:02pm | #17244

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.