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

Struts DispatchAction Tutorial

06.14.2012
| 75608 views |
  • submit to reddit

DispatchAction 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 class UserAction extends org.apache.struts.actions.DispatchAction. This class does not provide an implementation of the execute() method as the normal Action class does. The DispatchAction uses the execute method to manage delegating the request to the individual methods based on the incoming request parameter. For example if the incoming parameter is "method=add", then the add method will be invoked. These methods should have similar signature as the execute method.

public class UserAction extends DispatchAction {

    private final static String SUCCESS = "success";

    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 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>

Now lets see how to invoke a DispatchAction from jsp. We have a simple form with three buttons to add, update and delete a user. When each button is clicked a different method in UserAction class is invoked.

<html>
<head>
<script type="text/javascript">
function submitForm()
{
document.forms[0].action = "UserAction.do?method=add"
document.forms[0].submit();
}
</script>
</head>
<body>
<html:form action="UserAction" >
<table>
<tr>
    <td>
        <bean:write name="UserForm" property="message" />
    </td>
</tr>
<tr>
    <td>
        <html:submit value="Add" onclick="submitForm()" />
    </td>
</tr>
<tr>
    <td>
        <html:submit property="method" value="update" />
    </td>
</tr>
<tr>
    <td>
        <html:submit  property="method" >delete</html:submit>
    </td>
</tr>
</table>
</html:form>
</body>
</html>

Now consider the update and the delete button. The request parameter name specified in the action handler is "method". So this should be specified as the property name for the submit button. The name of the method to be invoked and the value of the button should be the same. So when the button is clicked the corresponding method in the UserAction will be called. The delete button shows an alternate way to specify the value of the button.

Here the main constraint is the method name and the button name should be same. So we can't have an update button like this "Update". Inorder to avoid this you can call a javascript function on click of the button. Specify the action and submit the form from javascript. In this way we can have a different button name and method name. On click of the Add button the action value is set to "UserAction.do?method=add" and the form is submitted from javascript.

On executing the sample example the following page is displayed to the user.

After clicking the add button the following page is displayed.

You can download the source code of the DispatchAction 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:

Comments

Joseph Williams replied on Thu, 2013/07/11 - 10:16pm

Where is the source code? NVM there is a link that I found in the tutorials, at the end of the outline that has all the src code.

Muralidhar Nayani replied on Wed, 2014/05/28 - 10:14am in response to: Joseph Williams

yes..you can find a link at the botoom...

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.