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 2003/05/14 19:10:54 UTC

DO NOT REPLY [Bug 19925] New: - Server side solution for DispatchAction and

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

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

Server side solution for DispatchAction and <input type="image" .../>

           Summary: Server side solution for DispatchAction and <input
                    type="image" .../>
           Product: Struts
           Version: Unknown
          Platform: Other
        OS/Version: Other
            Status: NEW
          Severity: Enhancement
          Priority: Other
         Component: Standard Actions
        AssignedTo: struts-dev@jakarta.apache.org
        ReportedBy: rtaylor@mulework.com


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-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-dev-help@jakarta.apache.org