Home > FAQs > Cookbook > Multiple Submit Buttons

You can be also interested in checking the page HTML form buttons HOWTO

Original solution

Introduction

Often, we have multiple submit buttons within a single form. The below is just a simple way of identifying which button was clicked, and which actions to take.

There are, of course, many ways of doing this, including the use of JavaScript to identify the actions, etc... You're welcome to pick and choose whichever method you find most useful. Struts 2 is flexible enough.

Form

<button type="submit" value="Submit" name="submit">
<button type="submit" value="Clear" name="clear">

Action with boolean properties

class MyAction extends ActionSupport {
   private boolean submit;
   private boolean clear;
   public void setSubmit(boolean submit) {
      this.submit = submit;
   }
   public void setClear(boolean clear) {
      this.clear = clear;
   }
   public String execute() {
      if (submit) {
         doSubmit();
         return "submitResult";
      }
      if (clear) {
         doClear();
         return "clearResult";
      }
      return super.execute();
   }
}

Explanation

The boolean properties 'submit' and 'clear' will be set to 'true' or 'false' according weather the submit or clear form element is present in the submitted form.

In this case, the properties are boolean, therefore the values set would be boolean.

There is another method, using String properties, described below...

Form

<button type="submit" value="Submit" name="buttonName">
<button type="submit" value="Clear" name="buttonName">

Action with String properties

class MyAction extends ActionSupport {
   private String buttonName;
   public void setButtonName(String buttonName) {
      this.buttonName = buttonName;
   }
   public String execute() {
      if ("Submit".equals(buttonName)) {
         doSubmit();
         return "submitResult";
      }
      if ("Clear".equals(buttonName)) {
         doClear();
         return "clearResult";
      }
      return super.execute();
   }
}

Explanation

In this case, the properties are String, therefore the values set are also String in nature.

I don't really like this method, as it ties in the Action to the Form. (What happens if you want different text to show up on the button ? You would have to change both the form as well as the corresponding action.)

Conclusion

There are other ways to achieve the same functionality. There are pros and cons to each methods. Feedback welcome.

Nyong Nyong's solution

This was originally taken from comment posted by Nyong Nyong to this page - you can see it here

The more elegant solution is probably by using multiple mappings for same Action. This way you don't need to set "struts.enable.DynamicMethodInvocation" to "true".

In JSP

<s:form method="post" action="mySubmitAction">
    <s:submit value="Submit"/>
    <s:submit value="Clear" action="myClearAction"/>
</form>

In struts.xml

<action name="mySubmitAction" class="MyAction" method="submit">
       <result>submit.jsp</result>
</action>
<action name="myClearAction" class="MyAction" method="clear">
       <result>submit.jsp</result>
</action>

Then in MyAction class

public String submit() throws Exception {
    // submit button logic here
    return SUCCESS;
}

public String clear() throws Exception {
    // clear button logic here
    return SUCCESS;
}


For best practice, if you have common data loaded / managed by your actions (submit & clear), then for example, you can define a MyBaseAction class, extended by MySubmitAction and MyClearAction class. Then this is how they looks like:

In struts.xml

<action name="mySubmitAction" class="MySubmitAction">
       <result>submit.jsp</result>
</action>
<action name="myClearAction" class="MyClearAction">
       <result>submit.jsp</result>
</action>

You don't need to specify a method name anymore, that means we will use the default execute() method.

Then in the MyAction, MySubmitAction and MyClearAction class

MyAction.java
public class MyAction extends ActionSupport {
    // common data or logic here
}
MySubmitAction.java
public class MySubmitAction extends MyAction {

    public String execute() throws Exception {
        // submit button logic here
        return SUCCESS;
    }
}
MyClearAction.java
public class MyClearAction extends MyAction {

    public String execute() throws Exception {
        // clear button logic here
        return SUCCESS;
    }
}

Credit: Andrea Ligios