You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by bu...@apache.org on 2005/02/18 23:13:09 UTC

DO NOT REPLY [Bug 33644] New: - New action forward dispatch action

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=33644>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=33644

           Summary: New action forward dispatch action
           Product: Struts
           Version: 1.2.4
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P3
         Component: Standard Actions
        AssignedTo: dev@struts.apache.org
        ReportedBy: pasek@gisdesign.de


In one of my last projects I occured the problem of having multiple submit
buttons on a single page and the need to have a dispatch mechanism not using
JavaScript on the client side.
After reading Struts documentation I found the solution of using the
LookupDispatchAction for this kind of problem but I was not quite satisfied with
it. The following things I do not like about the LookupDispatchAction:
- using multiple execute methods (add, delete) in one Action class. I like the
more fine grained approach (one processing step = one Action)
- the reusability of the dispatch class is poor. I had many forms for data input
with always the same buttons (create, update, delete). The dispatch action is
not reusable.

So I thought of a different solution using the ActionForward for dispatch. The
base idea is to have a reusable dispatcher Action that forwards to specific
Action implementations for each processing step.

My implementation which could be useful for all Struts users has the following
advantages:
- DispatcherAction class is reusable for multiple forms (with the same buttons).
- the Action classes can be fine grained (see above)
- better integration into Struts' conceptas using ActionForwards instead of
method calls using reflection.

Maybe you find this abstract class also useful and include it as common action
in future Struts releases.

The following documented source code fully reusing the LookupDispatchAction is
the result of my considerations:

package org.apache.struts.actions;

import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


/**
 *  <p>
 *  An abstract <strong>Action</strong> that dispatches to a
 *  <strong>ActionForward</strong> defined in <code>struts-config.xml</code>. 
 *  This is useful in cases where an HTML form has multiple submit buttons with 
 *  the same name and you want to keep your Action classes fine grained.
 *  The button name is specified by the <code>parameter</code> 
 *  property of the corresponding ActionMapping. To configure the use of this 
 *  action in your <code>struts-config.xml</code> file, create an entry like 
 *  this:</p> <pre>
 *   &lt;action path="/test"
 *           type="org.example.MyAction"
 *           name="MyForm"
 *          scope="request"
 *          input="/test.jsp"
 *      parameter="method"&gt;
 *    &lt;forward name="add" path="/save.do" /&gt;
 *    &lt;forward name="delete" path="/delete.do" /&gt;
 *   &lt;action/&gt;
 * </pre> <p>
 *
 *  which will use the value of the request parameter named "method" to locate
 *  the corresponding key in ApplicationResources. For example, you might have
 *  the following ApplicationResources.properties:</p> <pre>
 *    button.add=Add Record
 *    button.delete=Delete Record
 *  </pre><p>
 *
 *  And your JSP would have the following format for submit buttons:</p> <pre>
 *   &lt;html:form action="/test"&gt;
 *    &lt;html:submit property="method"&gt;
 *      &lt;bean:message key="button.add"/&gt;
 *    &lt;/html:submit&gt;
 *    &lt;html:submit property="method"&gt;
 *      &lt;bean:message key="button.delete"/&gt;
 *    &lt;/html:submit&gt;
 *  &lt;/html:form&gt;
 *  </pre> <p>
 *
 *  Your subclass must implement both getKeyMethodMap and the forward configurations
 *  should be defined in the struts-config.xml.
 * . An example of such implementations are:</p>
 * <pre>
 *  protected Map getKeyMethodMap() {
 *      Map map = new HashMap();
 *      map.put("button.add", "add");
 *      map.put("button.delete", "delete");
 *      return map;
 *  }
 * </pre>
 *  <p>
 *
 *  <strong>Notes</strong> - 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. You can override the
 *  method <code>unspecified</code> to provide a custom handler. If the submit
 *  was cancelled (a <code>html:cancel</code> button was pressed), the custom
 *  handler <code>cancelled</code> will be used instead.
 *
 */
public abstract class ForwardDispatchAction extends LookupDispatchAction {

    /**
     * Provides the mapping from resource key to ActionForward name.
     *
     * @return Resource key / ActionForward name map.
     */
    protected abstract Map getKeyForwardMap();

    /**
     * Implementation of abstract method for maximum reuse of 
     * <strong>LookupDispatchAction</strong>.
     * @return Resource key / method name map.
     */
    protected Map getKeyMethodMap()  {
        return getKeyForwardMap();
    }    
    
    /**
     *  Process the specified HTTP request, and create the corresponding HTTP
     *  response (or forward to another web component that will create it).
     *  Return an <code>ActionForward</code> instance describing where and how
     *  control should be forwarded, or <code>null</code> if the response has
     *  already been completed.
     *
     * @param mapping The ActionMapping used to select this instance
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     * @param form The optional ActionForm bean for this request (if any)
     * @return Describes where and how control should be forwarded.
     * @exception Exception if an error occurs
     */
    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response)
        throws Exception {

        if (isCancelled(request)) {
            ActionForward af = cancelled(mapping, form, request, response);
            if (af != null) {
                return af;
            }
        }
        // Identify the request parameter containing the method name
        String parameter = mapping.getParameter();
        if (parameter == null) {
            String message = messages.getMessage("dispatch.handler",
mapping.getPath());
            throw new ServletException(message);
        }

        // Identify the string to lookup
        String forwardName = getMethodName(mapping, form, request, response,
parameter);
        return mapping.findForward(forwardName);
    }
    
}

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org