Meyyappan has posted 101 posts at DZone. View Full User Profile

Struts LookupDispatchAction Tutorial

06.14.2012
| 47502 views |
  • submit to reddit

LookupDispatchAction provides a mechanism for grouping a set of related functions into a single action, thus eliminating the need to create seperate actions for each functions. In this example we will see how to group a set of user related actions like add user, update user and delete user into a single action called UserAction.

The LookupDispatchAction class extends org.apache.struts.actions.DispatchAction. Our class UserAction class extends LookupDispacthAction. This class does not provide an implementation of the execute() method as the normal Action class does. The LookupDispatchAction uses the execute method to manage delegating the request to the individual methods based on the incoming request parameter.

public class UserAction extends LookupDispatchAction {

    private final static String SUCCESS = "success";

    protected Map getKeyMethodMap() {
        Map map = new HashMap();
        map.put("UserForm.add", "add");
        map.put("UserForm.update", "update");
        map.put("UserForm.delete", "delete");
        return map;
    }

    public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        UserForm userForm = (UserForm) form;
        userForm.setMessage("Inside add user method.");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        UserForm userForm = (UserForm) form;
        userForm.setMessage("Inside update user method.");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        UserForm userForm = (UserForm) form;
        userForm.setMessage("Inside delete user method.");
        return mapping.findForward(SUCCESS);
    }

}

If you notice the signature of the add, update and delete methods are similar to the execute method except the name. The UserAction class must provide an implementation of getKeyMethodMap() method. This method maps the methods in the action class to keys in the Struts resource bundle file. The next step is to create an action mapping for this action handler. The request parameter name is specified using the parameter attribute. Here the request parameter name is method.

<action-mappings>
    <action input="/index.jsp" parameter="method" name="UserForm" path="/UserAction" scope="session" type="com.vaannila.UserAction">
        <forward name="success" path="/index.jsp" />
    </action>
</action-mappings>

getKeyMethodMap()

The getKeyMethodMap() method contains a HashMap. The names in the resource bundle file are the keys for this map and the corresponding values are the method names in the action class. For example the key value in the ApplicationResource.properties file is "UserForm.add" and the corresponding method name is "add" in the UserAction class. The main constraint in the DispatchAction is the method name in the action class and the button name in the jsp page should be the same. But here in LookupDispatchAction, we can have different names for the buttons and the methods.

In ApplicationResource.properties file each key is mapped to a value, that value represents the button name in the jsp page. In the getKeyMethodMap() method the same key is mapped to a different value, this value corresponds to the method name to be invoked in the action class.

ApplicationResource.properties
------------------------------
UserForm.add = Add
UserForm.update = Update
UserForm.delete = Delete
UserAction.java
---------------
protected Map getKeyMethodMap() {
    Map map = new HashMap();
    map.put("UserForm.add", "add");
    map.put("UserForm.update", "update");
    map.put("UserForm.delete", "delete");
    return map;
}

Here "Add", "Update" and "Delete" are the button names and "add", "update" and "delete" are the corresponding method names. If you want to change the name of the button at the later stage, you can easily do so by just changing the value in the ApplicationResource.properties file without making any changes to the jsp page.

The struts-config.xml file contains the following action mapping.

<action-mappings>
    <action input="/index.jsp" name="UserForm" parameter="method" path="/UserAction" scope="session" type="com.vaannila.UserAction">
        <forward name="success" path="/index.jsp" />
    </action>
</action-mappings>

The value of the parameter attribute of the action tag will be used as the request parameter and it's value will determine which method in the action class will be invoked. For example when the "Add" button is clicked the request parameter value will be "method=UserForm.add" and it will invoke the corresponding "add" method in the UserAction.

On clicking the add button the following page is displayed.

You can download the source code of the Struts LookupDispatchAction example by clicking on the Download link below.

Source :Download
Source + Lib :Download


Published at DZone with permission of its author, Meyyappan Muthuraman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: