You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Robert Taylor <rt...@mulework.com> on 2003/05/14 19:10:07 UTC

[PATCH] FW: Server side solution for DispatchAction and

Sorry for cross posting, but it was recommended that I submit this
patch/enhancement
here and then enter it as an enhancement in Bugzilla.

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

robert

-----Original Message-----
From: Brandon Goodin [mailto:mail@phase.ws]
Sent: Wednesday, May 14, 2003 10:37 AMatc
To: Struts Users Mailing List
Subject: RE: Server side solution for DispatchAction and <input
type="image" .../>


You should submit this to the developers list and add an enhancement request
to bugzilla. Otherwise, it will get lost in the shuffle. It looks like a
handy addition.

Brandon Goodin

-----Original Message-----
From: Robert Taylor [mailto:rtaylor@mulework.com]
Sent: Wednesday, May 14, 2003 8:23 AM
To: struts-user@jakarta.apache.org
Subject: Server side solution for DispatchAction and <input type="image"
.../>


I posted to the list a while back to see if Struts/anyone had a server side
solution for using DispatchAction and forms with images as submit buttons.
http://www.mail-archive.com/struts-user@jakarta.apache.org/msg33005.html

>>From what I have read a common solution is to use Javascript to set a
hidden
parameter whose name is defined in the action elements parameter attribute.

I haven't found that Struts or any extension supports a server side
solution.

In any case, I went ahead and extend the functionality of DispathAction to
solve this problem and called it ImageButtonDispatchAction. It allows you
to defined a comma delimited list of method names in the action element
parameter attribute. When invoked, it will iterate over that list and
return the first match found in the request parameter map.

I've posted the code below and would be interested in any feedback.
If there is already a known server side solution for this, I would still
be interested.

robert


<code>
package org.apache.struts.actions;

import java.util.StringTokenizer;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 * <code>ImageButtonDispatchAction</code>
 *
 * When a form is submitted using one or many
 * <input name="buttonName" type="submit" .../>
 * then the button name/value that was clicked is sent to
 * the server. Using the normal DispatchAction, an Action
 * parameter identifies the parameter name under which this
 * operation will be identified.
 *
 * When a form is submitted using one or many
 * <input name="buttonName" type="image" src="" .../> then
 * the button names/values that was clicked is sent to the
 * server. The difference is that there are two names and two
 * values sent to the server for the single button that
 * was clicked; buttonName.x and buttonName.y both exist in
 * the request along with their corresponding x-y coordinate
 * values. It becomes evident that the x-y coordinates provide
 * no value in helping to determine which operation to invoke
 * but it is the actual buttonName.x or buttonName.y that hints
 * to the operation to invoke.
 *
 * One solution to this problem is to use Javascript on the client
 * and set a hidden parameter value with the appropriate operation
 * to be invoked based on which input button was selected. This
 * presents an obvious problem if the client has Javascript disabled.
 *
 * <code>ImageButtonDispatchAction</code> solves this problem on the
 * server side. It gets the mutually exclusive list (comma delimited)
 * of operations to be invoked from the action elements parameter
 * attribute and sequentially looks for each operation name in the
 * request parameter map. If found, it stops searching and returns
 * the operation name to be invoked.
 *
 *
 * @author Robert Taylor <rt...@mulework.com>
 * @version $Revision: 1.4 $ $Date: 2003/02/28 18:44:07 $
 */

public class ImageButtonDispatchAction
		extends DispatchAction {

   	private static Logger logger = Logger.getLogger(
			ImageButtonDispatchAction.class.getName());
	private static final String DELIM = ",";
	private static final String IMAGE_ID = ".x";

	 /**
     * 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 form The optional ActionForm bean for this request (if any)
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     *
     * @exception Exception if an exception occurs
     */
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
        throws Exception {

        // Identify the request parameter containing the method name
        String parameter = mapping.getParameter();
        if (parameter == null) {
            String message =
                messages.getMessage("dispatch.handler", mapping.getPath());
            log.error(message);
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                               message);
            return (null);
        }

        // Identify the method name to be dispatched to.
        // dispatchMethod() will call unspecified() if name is null
        //String name = request.getParameter(parameter);
        String name = getOperation(parameter, request);

        // Invoke the named method, and return the result
        return dispatchMethod(mapping, form, request, response, name);
    }


	/**
	 * Return the operation name to be invoked if found
       * in the request
	 *
	 * @param parameter the mapping.getParameter() value
	 * @param request contains the parameter map
	 * @return the operation name to be invoked or null
       * if none is found.
	 * @exception throws NullPointerException if parameter
	 * or request is null.
	 */
	public static String getOperation(String parameter, HttpServletRequest
request) {

  		String operation = null;
  		StringTokenizer st = new StringTokenizer(parameter, DELIM);
  		Map parameterMap = request.getParameterMap();
  		boolean found = false;
  		StringBuffer buf = new StringBuffer(100);

		/*
  		 * Iterate through parameters and
  		 * look for a match in the request
  		 */
  		while (!found && st.hasMoreTokens()) {

  			String temp = st.nextToken().trim();
  			buf.setLength(0);
  			buf.append(temp).append(IMAGE_ID);

  			found = parameterMap.containsKey(buf.toString());

  			if (found) {

  				operation = temp;

  			}

  		}

		return operation;

	}


}
</code>


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



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


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


RE: [PATCH] FW: Server side solution for DispatchAction and

Posted by Steve Raeburn <st...@ninsky.com>.
Could you not do this more simply using the following
method?

    ImageButtonDispatchAction extends DispatchAction ETC...

	public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response)
        throws Exception {

        // Identify the method name to be dispatched to.
        // dispatchMethod() will call unspecified() if name
is null
        String name = null;
        for (Enumeration parms =
request.getParameterNames(); parms.hasMoreElements(); ) {
            String parm = (String) parms.nextElement();
            if (parm.endsWith(".x")) {
                name = parm.substring(0, parm.length() - 2);
		    break;
            }
        }

        // Invoke the named method, and return the result
        return dispatchMethod(mapping, form, request,
response, name);
    }

It differs from Robert's version in that it does not require
a parameter to be set in the action mapping configuration.
It just searches for a parameter that ends with ".x" and
get's the method name to execute from there.

Steve



-----Original Message-----
From: Robert Taylor [mailto:rtaylor@mulework.com]
Sent: May 14, 2003 10:10 AM
To: struts-dev@jakarta.apache.org
Subject: [PATCH] FW: Server side solution for DispatchAction
and <input
type="image" .../>


Sorry for cross posting, but it was recommended that I
submit this
patch/enhancement
here and then enter it as an enhancement in Bugzilla.

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

robert


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