Child pages
  • HTML form buttons HOWTO
Skip to end of metadata
Go to start of metadata

This HOWTO will describe the usage of HTML form buttons to invoke different behavior in actions.

Using different methods

The method attribute of the submit tag can be used to implement buttons that submit to different methods.

These boolean Properties can be tested to determine which button was pressed:

<form action="MyAction.action">
  <s:submit method="save" value="Save"/> 
  <s:submit method="delete" value="Delete"/> 
public class MyAction extends Action {
    public String save() {
        message = "The save button was pressed";
        return SUCCES;

    public String delete() {
        message = "The delete button was pressed";
        return SUCCES;
    // Output parameters

    private String message;
    public String getMessage() {
        return message;

Note: Do not use String properties with buttons and test for the value that's set. This will break as soon as the value attribute of the HTML button changes! This is likely because the value attribute is used as the button text.

Dynamic Set of Buttons

Consider a web page showing a shopping cart or similiar tabular data. Often there is a button belonging to each row, in case of the shopping cart a delete button to remove the item from the cart. The number of buttons is dynamic and the id that couples the button to an item cannot go to the value attribute because all buttons should read "delete".

The solution is to name the buttons like delete[123], delete[594], delete[494] where 123, 594 and 494 are, for example, item ids.

<form action="UpdateCart.action">
  <s:iterate value="items">
    <s:property value="name"> 
    <input type="submit" name="delete[<s:property value='id'>]" value="delete" /> <br/>

When the button for the item with the property id == "27" is pressed, a parameter named delete[27] and value "delete" is set in your action. The trick is to declare your action's "delete" property as a java.util.Map. Then, a key will exist for the button that was pressed.

public void class UpdateCart implements Action {

    // Must be initialized to be usable as a Struts 2 input parameter.
    private Map delete = new HashMap(); 

     * This is somewhat counter intuitive. But a property like "delete[OS:27]"
     * that is set to "delete" by Struts 2 will be interpreted by the underlying    
     * OGNL expression engine as "set the property 27 of the action's property
     * "delete" to the value "delete". So we must provide a getter for this
     * action. A setter is not needed.
    public Map getDelete() {
        return delete;

    public String execute() {
        for (Iterator i = delete.keySet().iterator(); i.hasNext(); ) {
            String id = (String);
            // do what ever you want

In this case it would not be necessary to iterate the whole keySet because it contains only one key but the same code can be use to handle sets of checkboxes if this is prefered later:

<form action="UpdateCart.action">
  <s:iterator value="items">
    <s:property value="name"> 
    <input type="checkbox" name="delete[<s:property value='item'/>]" value="delete"/> <br/>
  <input type="submit" name="updateCart" value="Update the cart"/>

The two implementations can even be combined two provide a quick "delete this item" button and a set of checkboxes for "mass updates". All with the above code, cool eh?


  • No labels

1 Comment

  1. Unknown User (drewmca)

    It looks more recent versions of xwork (1.0.5) don't handle this the way it used to be handled. If you have a boolean (simple datatype) property, it won't get set to true if you follow the steps above. But if you have a Boolean (object) property, it does, or at least it sets an actual Boolean object. In my usage, that Boolean object is Boolean.FALSE, not Boolean.TRUE, as expected, but at the very least you can check for a non-null value. If a non-null value is set, then you know someone tried to set that field, so if you're treating it as a true/false check , then you know that it's true because a value was set, even though the set value is Boolean.FALSE.