StrutsCatalog: This is an efficient way to end forever that pesky and recurrent problem of how to use multiple image buttons in your forms.

Assume that you have code not unlike:

<input type='image' name='update' src='change.gif'>
<input type='image' name='delete' src='nuke.gif'>

or, in Struts' image tag:

<html:image property='submit' src='change.gif'/>
<html:image property='delete' src='nuke.gif'/>

Now, how do we know which image has been clicked? The answer has been complicated and costly in the past. Here is a simple way to achieve everything at a low cost and with great flexibility and freedom.

public class ImageTagUtil {
  public static String getName(HttpServletRequest request) {
    String command = null;
    String buttonValue = null;
    Enumeration enum = request.getParameterNames();

    while(enum.hasMoreElements()) {
      buttonValue = (String)enum.nextElement();
      if(buttonValue.endsWith(".x")) {
        command = buttonValue.substring(0,buttonValue.indexOf('.'));
      }
    }
    return command;
  }
}

So, don't use buttons, but just mine the value of the [name].x request parameter.

Michael McGrady

(N.B.: This solution also works for non-image (submit) buttons by using values like <input type="submit" name="add.x" value="add"> in the submit button. Or, just use <input type="submit" name="button" value="add"> for a different solution for <input type="submit"> versus <input type="image">)

StrutsCatalogMultipleImageTagsSimplified (last edited 2009-09-20 23:12:16 by localhost)