Date: 2004-07-30T00:53:49
   Editor: MichaelMcGrady <[EMAIL PROTECTED]>
   Wiki: Apache Struts Wiki
   Page: StrutsCatalogMultipleImageButtonsWithNoJavaScript
   URL: http://wiki.apache.org/struts/StrutsCatalogMultipleImageButtonsWithNoJavaScript

   no comment

Change Log:

------------------------------------------------------------------------------
@@ -1,106 +1,82 @@
 StrutsCatalog: '''Here is one way to take care of that pesky and recurrent problem of 
how to use multiple image buttons in your forms.  This solution will suggest other 
possibilities you might want to code for yourself.'''
 
-The Struts "HTML" is simple.  If you have buttons you want called "submit" and 
"clear" for example, you would have the following page tags:
+The Struts page tags are simple.  If you have buttons you want called "submit" and 
"clear" for example, you would have the following page tags:
 
 {{{
   <html:image property='button.submit' src='Submit.gif'>
   <html:image property='button.clear' src='Clear.gif'>
 }}}
 
-The resultant HTML is:
+The resultant HTML producted by the magic html-image tag is:
 
 {{{
   <input type=image name='button.submit' src='Submit.gif'>
   <input type=image name='button.clear' src='Clear.gif'>
 }}}
 
-PLEASE NOTE THAT THE name attribute in the HTML or the property attribute in the 
Struts "HTML" image tag is the name of the command or operation, not the "name" of the 
button.  The "name" of the button, or "reference" of the button, is the src attribute. 
 Therefore, you could had the following as well:
+PLEASE NOTE THAT THE name attribute in the HTML or the property attribute in the 
Struts html-image tag is the name of the command or operation, not the "name" of the 
button.  The "name" of the button, or "reference" of the button, is the src attribute. 
 Therefore, you could use the following as well:
 
 {{{
   <html:image property='button.submit' src='CLICK.gif'>
   <html:image property='button.clear' src='GO_BACK.gif'>
 }}}
 
-Here's the button I use.
+I used to just use a button and encorporated various Commands like Submit Clear, etc. 
 Discussions with Larry Young of www.dalmatian.com convinced me that my solution was 
way to heavy-handed.  Now, like the bloke who was turned into a newt but "got betta", 
my new and improved solution is to provide a !ButtonForm to subclass or to copy, 
whatever excites you.  Here is the !ButtonForm:
 
 {{{
-public class Button
-    implements Serializable {
-  private Command clear  = new Command();
-  private Command submit = new Command();
-
-  public Button() {}
-  public Command getClear()  { return clear; }
-  public Command getSubmit() { return submit; }
+public class ButtonForm
+    extends ActionForm {
+  private CrackWillowButton button;
+  protected String command;
 
-  public class Command
-      implements Serializable {
-    private Integer x, y;
-
-    public Command()                 { }
-    public void setX(Integer x)      { this.x = x; }
-    public void setY(Integer y)      { this.y = y; }
-    public boolean pressed()         { return (x != null || y != null); }
-    public String toString()         { return "Button.Command: x = " + x + " y = " + 
y; }
+  public CrackWillowButton getButton() {
+    if(button == null) { this.button = new CrackWillowButton(); }
+    return button;
   }
-}
-}}}
-
-If you are using the Struts html image tag, you use the appropriate 
-attributes.  If you click on a button with the "button.submit" name in the HTML or 
the property in the Struts "HTML", the form will send the following, e.g., parameters 
in the request:
-{{{
-button.submit.x=37
-button.submit.y=3
-}}}
-
-The attributes "button.submit.x" and "button.submit.y"
-will be automatically set in the !ActionForm as follows in Struts:
-
-{{{
-  [ActionForm].getButton().getSubmit().setX(new Integer("37"));
-  [ActionForm].getButton().getSubmit().setY(new Integer("3"));
-}}}
-
-Your !ActionForm will contain the equivalent of the following code.
 
-{{{
-  // FIELD
-  protected Button button;
+  public String getCommand() {
+    String hold = command; command = null;
+    return hold;
+  }
 
-  // INITIALIZATION
-  public void reset(ActionMapping mapping,
-                    HttpServletRequest request) {
-    button = new Button();
+  public void reset() {
+    button = null;
   }
 
-  // GETTER METHOD: Struts calls the chosen Operation in 
-  // this Button and, then, setX(Integer) and setY(Integer).
-  // If you are not using Struts, you should do something
-  // equivalent.  We can tell the Operation chosen because
-  // only that Operation has x and y set.
-  public Button getButton() { return button; }
+  public class CrackWillowButton {
+    private Integer x, y;
+    public CrackWillowButton() { }
+    public CrackWillowButton getSubmit() { command = "submit";    return button; }
+    public CrackWillowButton getClear()  { command = "clear";    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; }
+    }
+  }
+} ///;-) 
 }}}
 
-Alternative frameworks can use alternative but equivalent mechanisms for
-setting the data on the button type pressed.  In the Action class,
-we check out which button type has been pressed as follows:
+
+In the Action class, we check out which button type has been pressed as follows:
 
 {{{
-  if([ActionForm].getButton().getSubmit().pressed()) {
+  String command = buttonForm.getCommand();
+  if("submit".equals(command)) {
     // do whatever
-  } else if ([ActionForm].getButton().getClear().pressed()) {
+  } else if ("clear".equals(command)) {
     // do whatever
   } else {
     // some assert code
   }
 }}}
 
-Again, alternative frameworks can use alternative but equivalent
-mechanisms for getting the data on the button type pressed.
-
-One last important matter.  The buttons are distinguished by what they do, not what 
they say.  You can have either "GO.gif" or "CLICK.gif" or "SUBMIT.gif" go with 
"button.submit" without changing the Action or !ActionForm classes.  And,  a new 
operation will require a change only in the Action class, not in the !ActionForm class.
-
-Enjoy!  Namaste!
+Enjoy!  And, thanks, Larry for spurring me on to better ideas!  Namaste!
 
 -- Michael !McGrady
 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to