Date: 2004-07-26T07:22:03
   Editor: MichaelMcGrady <[EMAIL PROTECTED]>
   Wiki: Apache Struts Wiki
   Page: StrutsCatalogMultipleImageButtonsWithNoJavaScript
   URL: http://wiki.apache.org/struts/StrutsCatalogMultipleImageButtonsWithNoJavaScript

   no comment

Change Log:

------------------------------------------------------------------------------
@@ -1,12 +1,9 @@
-StrutsCatalog: '''I don't know about you ..., but ... I like code.  I hate HTML.  
Here is a way to take care of that recurrent problem of how to get around the browser 
limitations and use multiple image buttons that crops up continually on the lists.'''
+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.'''
 
-This code is the composite of the work of lots of people and they also built on those 
before them.  References to them is at the bottom, but I wanted to acknowledge this at 
the start.  
-
-The key to the below is that the image tag will send 
button.submit.x=19&button.submit.y=10 and the properties button.submit.x and 
button.submit.y will be set as follows:
+The HTML is simple: 
 
 {{{
-    [ActionForm].getButton().getSubmit().setX(new Integer("19"));
-    [ActionForm].getButton().getSubmit().setX(new Integer("10"));
+  <input type=image name='button.submit' src='Submit.gif'>
 }}}
 
 Here's the button I use.
@@ -14,229 +11,115 @@
 {{{
 public class Button
     implements Serializable {
-  private String  name = null;
-  private Integer x    = null;
-  private Integer y    = null;
-
-  public Button() {
-  }
-
-  public Button(String name) {
-    this.name = name;
-  }
-
-  public boolean pressed() {
-    return (x != null || y != null);
-  }
-
-  public String getName() {
-    return name;
-  }
-
-  public Integer getX() {
-    return x;
-  }
 
-  public Integer getY() {
-    return y;
-  }
-
-  public void setName(String name) {
-    this.name = name;
-  }
-
-  public void setX(Integer x) {
-    this.x = x;
-  }
-
-  public void setY(Integer y) {
-    this.y = y;
-  }
-
-  public String toString() {
-    return "Button: name = " + name + " x = " + x + " y = " + y;
+  private Data cancel       = new Data("cancel");
+  private Data change       = new Data("change");
+  private Data clear        = new Data("clear");
+  private Data commitChange = new Data("commitChange");
+  private Data exit         = new Data("exit");
+  private Data reset        = new Data("reset");
+  private Data select       = new Data("select");
+  private Data submit       = new Data("submit");
+
+  public Button() {}
+  public Data getCancel()       { return cancel; }
+  public Data getChange()       { return change; }
+  public Data getClear()        { return clear; }
+  public Data getCommitChange() { return commitChange; }
+  public Data getExit()         { return exit; }
+  public Data getReset()        { return reset; }
+  public Data getSelect()       { return select; }
+  public Data getSubmit()       { return submit; }
+
+  public class Data
+      implements Serializable {
+    private String  name;
+    private Integer x, y;
+
+    public Data()                    { }
+    public Data(String name)         { this.name = name; }
+    public void setName(String name) { this.name = name; }
+    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.Data: name = " + name + " x = " 
+ x + " y = " + y; }
   }
+}
 }}}
 
-Here's the class that classifies the buttons into the operations they represent:
-
+If you are using the Struts html image tag, you use the appropriate 
+attributes.  The form will send the following, e.g., parameters:
 {{{
-public class ButtonOperation
-    implements Serializable {
-
-  protected Button cancel       = new Button("cancel");
-  protected Button change       = new Button("change");
-  protected Button clear        = new Button("clear");
-  protected Button commitChange = new Button("commitChange");
-  protected Button exit         = new Button("exit");
-  protected Button reset        = new Button("reset");
-  protected Button select       = new Button("select");
-  protected Button submit       = new Button("submit");
-
-  public ButtonOperation() {
-  }
-
-  public Button getCancel() {
-    return cancel;
-  }
-
-  public Button getChange() {
-    return change;
-  }
-
-  public Button getClear() {
-    return clear;
-  }
-
-  public Button getCommitChange() {
-    return commitChange;
-  }
-
-  public Button getExit() {
-    return exit;
-  }
-
-  public Button getReset() {
-    return reset;
-  }
-
-  public Button getSelect() {
-    return select;
-  }
-
-  public Button getSubmit() {
-    return submit;
-  }
-}
+button.submit.x=37
+button.submit.y=3
 }}}
 
-Here's the code !ActionForm for this action:
+The attributes "button.submit.x" and "button.submit.y"
+will be automatically set in the !ActionForm as follows in Struts:
 
 {{{
-
-public class LogonForm
-    extends ActionForm {
-  protected ButtonOperation button = null;
-
-  public LogonForm() {
-  }
-
-  public void reset(ActionMapping mapping, HttpServletRequest request) {
-    button   = new ButtonOperation();
-  }
-
-  public ButtonOperation getButton() { return button; }
-  public void setButton  (ButtonOperation button) { this.button = button; }
-} ///;-) Michael McGrady HomeSites
+  [ActionForm].getButton().getSubmit().setX(new Integer("9"));
+  [ActionForm].getButton().getSubmit().setX(new Integer("8"));
 }}}
 
-Here's an Action using these buttons for a registration form:
+Your !ActionForm will contain the equivalent of the following code.
 
 {{{
-public class ButtonAction extends Action {
-
-  public ActionForward execute(ActionMapping mapping,
-                               ActionForm form,
-                               HttpServletRequest request,
-                               HttpServletResponse response)
-      throws Exception {
-    ActionForward forward = 
ActionForwardLayout.getInputForward(mapping.getInputForward(), request);
-    ButtonForm pageForm = (ButtonForm) form;
-
-    if (pageForm.getButton().getSubmit().pressed()) {
-      this.executeRegistration(mapping, form, request, response);
-    } else if (pageForm.getButton().getClear().pressed()) {
-      pageForm.reset(mapping, request);
-    } else if (pageForm.getButton().getCancel().pressed()) {
-      forward = mapping.findForward("cancel");
-    }
+  // FIELD
+  protected Button button;
 
-    return forward;
+  // INITIALIZATION
+  public void reset(ActionMapping mapping,
+                    HttpServletRequest request) {
+    button = new Button();
   }
 
-  public ActionForward executeRegistration(ActionMapping mapping,
-                                           ActionForm form,
-                                           HttpServletRequest request,
-                                           HttpServletResponse response)
-      throws Exception {
-
-    // example only
-    return null;
-  }
-}
+  // GETTER and SETTER METHODS
+  public Button getButton() { return button; }
+  public void   setButton(Button button) { this.button = button; }
 }}}
 
-Here is the struts action mapping:
+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:
 
 {{{
-<action 
- path="/button"
-  type="com.crackwillow.struts.action.ButtonAction"
-  name="ButtonForm"
-  scope="request"
-  input="/button_test.jsp">
-  <forward 
-    name="cancel" 
-    path="/button_test.jsp" 
-    redirect="true"/>
-</action>
+  if([ActionForm].getButton().getSubmit().pressed()) {
+    // do whatever
+  }
 }}}
 
-Here's the struts form definition:
+Again, alternative frameworks can use alternative but equivalent
+mechanisms for getting the data on the button type pressed.
 
-{{{
-<form-beans>
-  <form-bean
-    name="ButtonForm"
-    type="com.crackwillow.struts.form.ButtonForm" />
-</form-beans>
-}}}
 
-Here's an example usage:
 
-{{{
-<html:form 
-  name="ButtonForm" 
-  type="com.crackwillow.struts.form.ButtonForm" 
-  method="get"
-  action='/button.do'>
+Enjoy!  Namaste!
 
-  FIRST NAME:   <input type="text" name="firstName" value=""><BR>
-  LAST NAME:    <input type="text" name="lastName" value=""><BR>
-  EMAIL:        <input type="text" name="email" value=""><BR>
+-- Michael !McGrady
 
-  <html:image property="button.submit" src="submit.gif"/><BR>
-  <html:image property="button.clear" src="clear.gif"/><BR>
-  <html:image property="button.cancel" src="cancel.gif"/><BR>
-  <html:image property="button.reset"src="reset.gif"/>
[EMAIL PROTECTED]
 
-</html:form>
-}}}
+LITERATURE
 
 See the following references and many more in all the lists which contain all this 
and more you will need to expand on this idea.  
-
+{{{
 http://www.jguru.com/faq/view.jsp?EID=543699 
+}}}
 
 Read all the responses to Ted Husted's remarks as well as his insights.  They are 
also helpful.
-
+{{{
 http://www.codetoad.com/html/buttons/image_submit_button.asp 
 
 http://www.jguru.com/faq/view.jsp?EID=893423
-
+}}}
 Here is some more of Ted's work and this is directly related to our present solution. 
 
-
+{{{
 http://j2ee.lagnada.com/struts/html-buttons.htm
-
+}}}
 Watch out for the little mistakes in this one, but this is mostly code and the source 
of the main idea in what we have done.  But, this is essentially the pattern we 
employ, which is a standard pattern, of course.  
-
+{{{
 http://www.w3.org/TR/REC-html40/interact/forms.html
 http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_20804830.html
 http://www.cs.tut.fi/~jkorpela/forms/imagebutton.html
-
-Enjoy!  Namaste!
-
--- Michael !McGrady
-
[EMAIL PROTECTED]
-
[EMAIL PROTECTED]
+}}}

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

Reply via email to