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/19 17:31:07 UTC

[Apache Struts Wiki] Updated: StrutsCatalogMultipleImageButtonsWithNoJavaScript

   Date: 2004-09-19T08:31:07
   Editor: MichaelMcGrady <mi...@michaelmcgrady.com>
   Wiki: Apache Struts Wiki
   Page: StrutsCatalogMultipleImageButtonsWithNoJavaScript
   URL: http://wiki.apache.org/struts/StrutsCatalogMultipleImageButtonsWithNoJavaScript

   1092195460

Change Log:

------------------------------------------------------------------------------
@@ -1 +1,210 @@
-Go to StrutsCatalogMultipleImageTagsSimplified
+StrutsCatalog: '''Here are TWO WAYS 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.  Use the one you like best.'''
+
+First, you can merely mine the parameterNames of the request and build your logic in your processing of the request accordingly.
+
+{{{
+public final class ButtonCommand {
+  private ButtonCommand() {
+  }
+
+  public final static String getCommand(HttpServletRequest request) {
+    Enumeration enum = request.getParameterNames();
+    String parameterName = null;
+    while(enum.hasMoreElements()) {
+      parameterName = (String)enum.nextElement();
+      if(parameterName.endsWith(".x")) {
+        return parameterName.substring(0,parameterName.indexOf('.'));
+      }
+    }
+    return parameterName;
+  }
+}
+}}}
+
+I use this with the following simple logic in a processing class called by my Action class.  You can do whatever you like to use the results of the !ButtonCommand class.  You don't, of course, have to have a Struts solution to use this.
+
+{{{
+String command = ButtonCommand.getCommand(request);
+if (ButtonConstant.SUBMIT.equals(command)) {
+  // do whatever
+}
+}}}
+
+My !ButtonConstant class is equally simple.  You might have, for example:
+
+{{{
+public final class ButtonConstant {
+    public static final String CLEAR  = "clear";
+    public static final String DELETE = "delete";
+    public static final String SUBMIT = "submit";
+  private ButtonConstant() {
+  }
+}
+}}}
+
+If you want something more OOP in nature, then the following might be your choice.
+
+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 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 use the following as well:
+
+{{{
+  <html:image property='button.submit' src='CLICK.gif'>
+  <html:image property='button.clear' src='GO_BACK.gif'>
+}}}
+
+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 (if you want more buttons, just add to the getters, e.g. getSubmit():
+
+{{{
+public class ButtonForm
+    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 reset() {
+    button = null;
+  }
+
+  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; }
+    }
+  }
+} ///;-) 
+}}}
+
+
+In the Action class, we check out which button type has been pressed as follows:
+
+{{{
+  String command = buttonForm.getCommand();
+  if("submit".equals(command)) {
+    // do whatever
+  } else if ("clear".equals(command)) {
+    // do whatever
+  } else {
+    // some assert code
+  }
+}}}
+
+Enjoy!  And, thanks, Larry, for spurring me on to better ideas!  Namaste!
+
+-- Michael !McGrady
+
+michael@michaelmcgrady.com
+
+----
+
+Just an extension. 
+
+Instead of checking the command value in the execute method of the Action, you can make your Action extend DispatchAction by overriding the protected dispatchMethod
+
+{{{
+public class TestAction extends DispatchAction {
+       /**
+        * Dispatch to the specified method.
+        * @since Struts 1.1
+        */
+       protected ActionForward dispatchMethod(
+               ActionMapping mapping,
+               ActionForm form,
+               HttpServletRequest request,
+               HttpServletResponse response,
+               String name)
+               throws Exception {
+               return super.dispatchMethod(mapping, form, request, response, 
+                                          ((ButtonForm) form).getCommand());
+       }
+
+       public ActionForward submit(
+               ActionMapping mapping,
+               ActionForm form,
+               HttpServletRequest request,
+               HttpServletResponse response) {
+               return mapping.getInputForward();
+       } 
+
+       // Add more methods corresponding to the number of images (submits)
+}
+
+}}}
+'''OR''' by overriding the execute method
+{{{
+public class TestAction extends DispatchAction {
+       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 = ((ButtonForm) form).getCommand();
+
+               // Invoke the named method, and return the result
+               return dispatchMethod(mapping, form, request, response, name);
+       }
+
+       public ActionForward submit(
+               ActionMapping mapping,
+               ActionForm form,
+               HttpServletRequest request,
+               HttpServletResponse response) {
+               return mapping.getInputForward();
+       }
+       
+       // Add more methods corresponding to the number of images (submits)
+}
+
+}}}
+
+If we are dealing with too many images, then it's better to make it an DispathAction similar to the one illustrated above. One thing to remember is to define a parameter in the ActionMapping.
+
+Thanks,
+Kishore Senji.

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