Monday, 11 April 2016

What is DispatchAction and LookupDispatchAction ?



DispatchAction
  • org.apache.struts.action.DispatchAction class
  • lets you combine Struts actions into one class, each with their own method.
  • allows multiple operation to mapped to the different functions in the same Action class.

Example
Create action class by extending it DispatchAction class and put multiple methods into it. Like :
public ActionForward create(ActionMapping mapping,  ActionForm form, 
  HttpServletRequest request, HttpServletResponse response) 
  throws IOException, ServletException { ...}

public ActionForward save(ActionMapping mapping, ActionFormform, 
  HttpServletRequest request, HttpServletResponse response) 
  throws IOException, ServletException { ...}

Specify the name of of the dispatch property as the "parameter" property of the action-mapping.
<action path="/reg/dispatch" type="app.reg.RegDispatch
        name="regForm" scope= "request" validate="true"
        parameter="dispatch"/>

Now, call this action by passing request parameter "dispatch" from the calling page.
URL ? dispatch = create
URL ? dispatch = save


LookupDispatchAction
  • An abstract Action that dispatches to the subclass mapped execute method.
  • 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.

Example
Put the multiple submit buttons with same names but different messages keys (message keys defined in message resources)
<html:form action="/test">
   <html:submit property="method">
      <bean: message key="button.add"/>
    </html:submit>
    <html:submit property="method">
      <bean:message key="button.delete"/>
    </html:submit>
</html:form>

Create action class by extending it LookupDispatchAction class.
Your subclass must implement both getKeyMethodMap and the methods defined in the map.
   public ActionForward add(...)  { ... }
   public ActionForward delete(...)   { ... }

   protected Map getKeyMethodMap() {
      Map map =  new HashMap();
      map.put("button.add", "add");
      map.put("button.delete", "delete");
      return map;
   }

Specify the name of of the dispatch property as the "parameter" property of the action-mapping.
<action path="/test" type="org.example.MyAction" name="MyForm
        scope="request" input="/test.jsp" parameter="method"/>

No comments:

Post a Comment

Note: only a member of this blog may post a comment.