Struts DispatchAction form with multiple buttons

DispatchAction provides a mechanism for grouping a set of related functions into a single action, thus eliminating the need to create separate 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.

FROM HERE

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s