Difference between DispatchAction and LookupdisactAction?

Posted on 2008-11-11
Last Modified: 2013-11-24
what are Difference between DispatchAction and LookupdisactAction?
both are used to eliminate of having separate action class for different function(add/update/delete)
then where to use DispatchAction  and where to use LookUpDispatchAction ?
Question by:Asheesh_Kumar
    LVL 23

    Assisted Solution


    Lets say you have 3 buttons on your form: add, delete, and update. If your website is in English and you're not planning to use internatoinalization, you would code the three buttons:

    <html:submit property="action">add</html:submit>
    <html:submit property="action">update</html:submit>
    <html:submit property="action">delete</html:submit>

    You could then use DispatchAction and code the methods add(), update(), and delete(), and Struts would forward to the appropriate method based on the button pressed.

    However, if you use internationalization, this won't work because the label of the button could be in any number of languages. In this case, you need to use LookupDispatch action and code your JSP like this:

    <html:submit property="action"><bean:message key="label.add" /></html:submit>
    <html:submit property="action"><bean:message key="label.update /></html:submit>
    <html:submit property="action"><bean:message key="label.delete" /></html:submit>

    You'll then code the getMethodKey() to find the appropriate method based on the message key. That way, regardless of what language is actually used at runtime, Struts will still dispatch to the correct method.

    I hope it explains...
    LVL 23

    Accepted Solution

    LookupDispatchAction is subclass of DispatchAction.
    In case of DispatchAction, you have to declare the method name in the JSP page.
    For Example :
    http://localhost:8080/emp/ // IN CASE OF DispatchAction
    here step=add , we are delaring method name in JSP.
    <html:submit property="step">Add</html:submit> //IN CASE OF DispatchAction
    so we can't use localization for button.

    To over come both the issues below
    a)method name declaration in JSP
    b)can't use localization for button
    We will go for LookupDispatchAction.

    In the LookupDispatchAction :
    For example :
    If there are three operation like add, delete, update employee.
    You can create three different Actions 
    AddEmpAction, DeleteEmpAction and UpdateEmpAction.
    This is a valid approach, although not elegant since there might be duplication of code across
    the Actions since they are related. LookupDispatchAction is the answer to this
    problem. With LookupDispatchAction, you can combine all three Actions into one.

    Example :
    Step 1.
    three buttons might be
    <html:submit property="step">
    <bean:message key="button.add"/>
    <html:submit property="step">
    <bean:message key="button.delete"/>
    <html:submit property="step">
    <bean:message key="button.update"/>

    //No need method name declaration in JSP . Button name from resource bundle.

    Step 2.
    In the the Resource Bundle. //Here you can add localization.

    Step 3.
    Implement a method named getKeyMethodMap() in the subclass of the
    LookupDispatchAction. The method returns a java.util.Map. The
    keys used in the Map should be also used as keys in Message Resource

    public class EmpAction extends LookupDispatchAction {

    public Map getKeyMethodMap()
    Map map = new HashMap();
    map.put("button.add", "add");
    map.put("button.delete", "delete");
    map.put("button.update", "update");

    public ActionForward add(ActionMapping mapping,
    ActionForm form, HttpServletRequest request,
    HttpServletResponse response) throws Exception
    //your logic

    public ActionForward delete(ActionMapping mapping,
    ActionForm form, HttpServletRequest request,
    HttpServletResponse response) throws Exception
    //your logic

    public ActionForward update(ActionMapping mapping,
    ActionForm form, HttpServletRequest request,
    HttpServletResponse response) throws Exception
    //your logic


    in struts-config.xml

    <action path="/empaction"
    <forward name="add-success"
    <forward name="delete-success"
    <forward name="update-success"

    for every form submission, LookupDispatchAction does the
    reverse lookup on the resource bundle to get the key and then gets the method
    whose name is associated with the key from

    Flow of LookupDispatchAction:
    a) Form the button name "Add" get the key "button.add" fro resource bundle.
    b) then in the Map getKeyMethodMap() find the value associated with the key "button.add".
    c) value from the Map is "add" . then call add() method of the same action class.


    Author Closing Comment

    satisfactory solution.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now