Tuesday, August 6, 2013

Struts LookupDispatchAction Tutorial

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.
01.public class UserAction extends LookupDispatchAction {
02. 
03.private final static String SUCCESS = "success";
04. 
05.protected Map getKeyMethodMap() {
06.Map map = new HashMap();
07.map.put("UserForm.add""add");
08.map.put("UserForm.update""update");
09.map.put("UserForm.delete""delete");
10.return map;
11.}
12. 
13.public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throwsException {
14.UserForm userForm = (UserForm) form;
15.userForm.setMessage("Inside add user method.");
16.return mapping.findForward(SUCCESS);
17.}
18. 
19.public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throwsException {
20.UserForm userForm = (UserForm) form;
21.userForm.setMessage("Inside update user method.");
22.return mapping.findForward(SUCCESS);
23.}
24. 
25.public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throwsException {
26.UserForm userForm = (UserForm) form;
27.userForm.setMessage("Inside delete user method.");
28.return mapping.findForward(SUCCESS);
29.}
30. 
31.}
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.
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>

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.
1.ApplicationResource.properties
2.------------------------------
3.UserForm.add = Add
4.UserForm.update = Update
5.UserForm.delete = Delete
01.UserAction.java
02.---------------
03.protected Map getKeyMethodMap() {
04.Map map = new HashMap();
05.map.put("UserForm.add""add");
06.map.put("UserForm.update""update");
07.map.put("UserForm.delete""delete");
08.return map;
09.}
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.
1.<action-mappings>
2.<action input="/index.jsp" name="UserForm" parameter="method"path="/UserAction" scope="session" type="com.vaannila.UserAction">
3.<forward name="success" path="/index.jsp" />
4.</action>
5.</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.

No comments:

Post a Comment