Stripes Multiple Submit Buttons

2008-07-20 @ admin

Funny coincidence, I was trying things out in Rails and was having trouble getting more than one submit button working in the same form. The coincidence part is that I also saw a posting about Struts2, and it turns out they don't have a good solution either. In Stripes, it's clean and simple.

There are solutions in Rails and Struts2 of course, but they are not clean where clean means not having to check against the value of the submit button with an if.

In Stripes, the name attribute of a submit button automatically calls the event handler method in the Action Bean with the same name. The value of the button, which is what the user sees, can be anything. So it's very clean and simple. In the JSP:

  <s:submit name="send" value="Press this to Send"/>
  <s:submit name="cancel" value="Press this to Cancel"/>

In the Action Bean:

  public Resolution send() {
    // user pressed Send button
    // do send action
    // return result
  }
  public Resolution cancel() {
    // user pressed Cancel button
    // do cancel action
    // return result
  }

Even better, using localized labels for the buttons is also very clean and simple. The Action Bean remains the same. In the JSP, you actually use less code:

  <s:submit name="send"/>
  <s:submit name="cancel"/>

You then put your localized labels in StripesResources.properties for the default locale and in StripesResources_xx.properties for other xx locales. For example:

StripesResources.properties:

  send=Press this to Send
  cancel=Press this to Cancel

StripesResources_fr.properties:

  send=Appuyez ici pour Envoyer
  cancel=Appuyez ici pour Annuler

Of course, you can use Unicode characters for the labels with the standard Java \unnnn notation.

Gotta love it!

Cheers,
Freddy

Purchase "The Stripes Book" now

Comments

  1. Oscar Westra van Holthe - Kind

    Nice find.

    Actually, the solution in Stripes will also work for forms submitted with the button tag (instead of the input tag with type=“submit”). I find it works better and more cleanly for two reasons:
    1. You don’t have to check against the (localized!) submitted value. You’ve already pointed out the advantage here.
    2. The approach ignores the submitted value, which for the button tag using Internet Explorer will be the tag’s content HTML (instead of it’s value). Microsoft even documents it deviates from the standard here.

    Nov 16


  2. iongion

    What about the validity of the html code?
    As i remember, multiple submit buttons on a form mess it up.

    Nov 16


  3. The HTML code is valid. From www.w3.org: “submit buttons: When activated, a submit button submits a form. A form may contain more than one submit button.”

    Ref: http://www.w3.org/TR/html401/interact/forms.html#submit-button

    Nov 16