Monday, January 10, 2011

LookupDispatchAction in Struts

This is an abstract Action that dispatches to the sub-class mapped execute method. This is useful in 
cases where an HTML form has multiple submit buttons with the same name. The button name is specified 
by the parameter property of the corresponding ActionMapping. So basically there are two usage: 
    a) Avoiding Method name declaration in JSP.
    b) Getting localization for buttons as Button levels are defined in Properties file.

To configure the use of this action in 
our struts-config.xml file, create an entry like this:

<action path="/test" type="org.example.MyAction" name="MyForm"
         scope="request" input="/test.jsp" parameter="method">        
    <forward name="add-success" path="addEmpSuccess.jsp" redirect="true"/>
    <forward name="delete-success" path="deleteEmpSuccess.jsp" redirect="true"/>
    <forward name="update-success" path="updateEmpSuccess.jsp" redirect="true"/>

And our JSP would have the following format for submit buttons:
<html:form action="/test">
    <html:submit property="method">
      <bean:message key="button.add"/>
    <html:submit property="method">
      <bean:message key="button.delete"/>

Which will use the value of the request parameter named "method" to locate the corresponding key in For example, we might have the following keys:
    button.add=Add Record
    button.delete=Delete Record 
    button.update=Update Record    

Our subclass must implement both getKeyMethodMap() and the methods defined in the map. An 
example of such implementations are:
public class EmpAction extends LookupDispatchAction {
    protected Map getKeyMethodMap() {
        Map map = new HashMap();
        map.put("button.add", "add");  //Or you can read properties file's keys using iterator
        map.put("button.delete", "delete");
        return map;

    public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, 
                HttpServletResponse response) throws Exception {
        // do add
        // Create a User DTO and copy the properties from the userForm 
        User user = new User();
        BeanUtils.copyProperties(user, form);      //(destination, source)
        DataSource dataSource = getDataSource(request, "userDB");
        Connection conn = dataSource.getConnection();        
        UserDAO dao = DAOFactory.createUserDAO(conn);        
        return mapping.findForward("success");        

    public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, 
                HttpServletResponse response) throws Exception {
        //your logic
        return mapping.findForward("delete-success");
    public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, 
                HttpServletResponse response) throws Exception {
        //your logic
        return mapping.findForward("update-success");
If duplicate values exist for the keys returned by getKeys, only the first one found will be returned. 
If no corresponding key is found then an exception will be thrown.

At last: Flow of LookupDispatchAction:
    For every form submission, LookupDispatchAction does the reverse lookup on the resource bundle to 
    get the key and then calls the method for fetching value using the key from getKeyMethodmap() method.

    a) Form the button name "Add" get the key "button.add" from resource bundle.
    b) then in the Map getKeyMethodMap() find the value associated with the key "button.add".
    c) value from the Map is "add", so call add() method of the same action class.

1 comment:

  1. nice tutorial ... it helps me a lot in my practice..
    I got I more tutorial which may help you too: