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

[Apache Struts Wiki] Updated: StrutsCatalogMultipleImageTagsSimplified

   Date: 2004-09-13T11:23:28
   Editor: MichaelMcGrady <mi...@michaelmcgrady.com>
   Wiki: Apache Struts Wiki
   Page: StrutsCatalogMultipleImageTagsSimplified
   URL: http://wiki.apache.org/struts/StrutsCatalogMultipleImageTagsSimplified

   no comment

Change Log:

------------------------------------------------------------------------------
@@ -1,4 +1,4 @@
-StrutsCatalog: '''This is an efficient way to end forever that pesky and recurrent problem of how to use multiple image buttons in your forms.  (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">)'''
+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:'''
@@ -16,21 +16,6 @@
 }}}
 
 '''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.'''
-
-{{{
-    String button = null;
-    Enumeration enum = request.getParameterNames();
-    String parameterName = null;
-    while(enum.hasMoreElements()) {
-      parameterName = (String)enum.nextElement();
-      if(parameterName.endsWith(".x")) {
-        button = parameterName.substring(0,parameterName.indexOf('.'));
-      }
-    }
-}}}
-
-'''There you go.  ''If you clicked the image with name='submit', then the variable button will have the value "submit".''  I suggest that you toss out the !LookupDispatchActions, the !ButtonCommands, etc.  This is a done deal.  You can clearly seek other ways to ensure a bit more safety.  For example, the code works equally as well with "submit.button" as it does with "submit".  Elegant, no, eh?  I use a class that encapsulates this functionality as follows:
-
 {{{
 public class ImageTagUtil {
   public static String getName(HttpServletRequest request) {
@@ -48,93 +33,10 @@
   }
 }
 }}}
-
-Surprisingly, some people still prefer the way I used to do this.  I think they just like the plain fanciness of it all.  For those people, here is code that is less extensible, more coupled, heavier, etc.  This code is comparatively not as good, in my opinion.  There is a further use of button objects which requires that you create a separate button object for each image button you use on an html page.  That solution is very heavy and significantly affects the footprint of a struts page.  ''If'' you are going to use buttons, ''which, again, is not recommended'', I would suggest the following more lightweight version, which creates and then nulls one button object per request.  The use of an inner class allows the necessary communications between the action form and the button class.
-
-{{{
-public class AdminButtonForm
-    extends ActionForm {
-  protected CrackWillowButton button;
-  protected String command;
-
-  public CrackWillowButton getButton() {
-    if(button == null) {
-      this.button = new CrackWillowButton();
-    }
-    return button;
-  }
-
-  public String getCommand() {
-    String hold = command;
-    command = null;
-    return hold;
-  }
-
-  public void setCommand(String command) {
-    this.command = command;
-  }
-
-  public void reset() {
-    button = null;
-  }
-
-  public class CrackWillowButton {
-    private Integer x, y;
-    public static final String CREATE   = "create";
-    public static final String RETRIEVE = "retrieve";
-    public static final String UPDATE   = "update";
-    public static final String DELETE   = "delete";
-    public static final String SUBMIT   = "submit";
-
-    public CrackWillowButton() {
-    }
-
-    public CrackWillowButton getCreate()    {
-      setCommand(this.CREATE);
-      return button;
-    }
-
-    public CrackWillowButton getRetrieve()    {
-      setCommand(this.RETRIEVE);
-      return button;
-    }
-
-    public CrackWillowButton getUpdate()     {
-      setCommand(this.UPDATE);
-      return button;
-    }
-
-    public CrackWillowButton getDelete() {
-      setCommand(this.DELETE);
-      return button;
-    }
-
-    public CrackWillowButton getSubmit() {
-      setCommand(this.SUBMIT);
-      return button;
-    }
-
-    public void setX(Integer x) {
-      if(y != null) {
-        reset();
-      } else {
-        this.x = x;
-      }
-    }
-
-    public void setY(Integer y) {
-      if(x != null) {
-        reset();
-      } else {
-        this.y = y;
-      }
-    }
-  }
-}
-}}}
 So, don't use buttons, but just mine the value of the [name].x request parameter.
 
 
 Michael !McGrady
-Cap't of the Eh Team'''
+
+(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">)'''
 

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