DispatchAction提供了一个分组到一个单一的行动的相关功能设置机制,从而消除了需要为每个功能的独立行动。在这个例子中,我们将看到如何设置一组用户添加用户有关的行动一样,更新成一个单一的行动称为UserAction用户和删除用户。
类UserAction扩展org.apache.struts.actions.DispatchAction。这个类不提供的执行()作为正常类的方法并实施行动。本的DispatchAction使用execute方法来管理下放的要求就传入的请求参数为基础的单个方法。例如,如果传入的参数是“方法=添加”,然后添加方法将被调用。这些方法应该有类似签名的执行方法。
01.
public
class
UserAction
extends
DispatchAction {
02.
03.
private
final
static
String SUCCESS =
"success"
;
04.
05.
public
ActionForward add(ActionMapping mapping, ActionForm form,
06.
HttpServletRequest request, HttpServletResponse response)
07.
throws
Exception {
08.
UserForm userForm = (UserForm) form;
09.
userForm.setMessage(
"Inside add user method."
);
10.
return
mapping.findForward(SUCCESS);
11.
}
12.
13.
public
ActionForward update(ActionMapping mapping, ActionForm form,
14.
HttpServletRequest request, HttpServletResponse response)
15.
throws
Exception {
16.
UserForm userForm = (UserForm) form;
17.
userForm.setMessage(
"Inside update user method."
);
18.
return
mapping.findForward(SUCCESS);
19.
}
20.
21.
public
ActionForward delete(ActionMapping mapping, ActionForm form,
22.
HttpServletRequest request, HttpServletResponse response)
23.
throws
Exception {
24.
UserForm userForm = (UserForm) form;
25.
userForm.setMessage(
"Inside delete user method."
);
26.
return
mapping.findForward(SUCCESS);
27.
}
28.
}
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.
1.
<
action-mappings
>
2.
<
action
input
=
"/index.jsp"
parameter
=
"method"
name
=
"UserForm"
path
=
"/UserAction"
scope
=
"session"
type
=
"com.vaannila.UserAction"
>
3.
<
forward
name
=
"success"
path
=
"/index.jsp"
/>
4.
</
action
>
5.
</
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.
1.
<
action-mappings
>
2.
<
action
input
=
"/index.jsp"
parameter
=
"method"
name
=
"UserForm"
path
=
"/UserAction"
scope
=
"session"
type
=
"com.vaannila.UserAction"
>
3.
<
forward
name
=
"success"
path
=
"/index.jsp"
/>
4.
</
action
>
5.
</
action-mappings
>
01.
<
html
>
02.
<
head
>
03.
<
script
type
=
"text/javascript"
>
04.
function submitForm()
05.
{
06.
document.forms[0].action = "UserAction.do?method=add"
07.
document.forms[0].submit();
08.
}
09.
</
script
>
10.
</
head
>
11.
<
body
>
12.
<
html:form
action
=
"https://youtubeproxy.org/default.aspx "
>
13.
<
table
>
14.
<
tr
>
15.
<
td
>
16.
<
bean:write
name
=
"UserForm"
property
=
"message"
/>
17.
</
td
>
18.
</
tr
>
19.
<
tr
>
20.
<
td
>
21.
<
html:submit
value
=
"Add"
onclick
=
"submitForm()"
/>
22.
</
td
>
23.
</
tr
>
24.
<
tr
>
25.
<
td
>
26.
<
html:submit
property
=
"method"
value
=
"update"
/>
27.
</
td
>
28.
</
tr
>
29.
<
tr
>
30.
<
td
>
31.
<
html:submit
property
=
"method"
>delete</
html:submit
>
32.
</
td
>
33.
</
tr
>
34.
</
table
>
35.
</
html:form
>
36.
</
body
>
37.
</
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.