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