0

I am devoloping a web application using J2EE and Spring Roo as framework.

I want to create a form with two submit buttons:

  • One for save and continue
  • Another for save and finish

    <form action="mycontroller" method="post"> <input type="submit" value="Save and continue"/> <input type="submit" value="Save and finish"/> </form>

So I can choose to either store the data in the database and add more entries or to store the data and finish the process.

How can I check what submit button was clicked in the method of the controller that processes the action?

public class MyController {
    void actionMethod(...) {
        // check which submit was clicked
    }
}
dabadaba
  • 9,064
  • 21
  • 85
  • 155
  • possible duplicate of [Multiple submit buttons in an HTML form](http://stackoverflow.com/questions/48/multiple-submit-buttons-in-an-html-form) – Bart May 26 '14 at 10:07
  • @Bart I am not asking that, I am asking how to decide in the controller which button was pressed. Read the question again. – dabadaba May 26 '14 at 10:36

2 Answers2

6

You should add a name field to both buttons:

<input type="submit" name="button" value="Save and continue"/>
<input type="submit" name="button" value="Save and finish"/>

Once in the controller, you can recover the element by this name field and check its value field:

String field = request.getParameter("button");

if ("Save and continue".equals(button)){
    // Do stuff
}
else if ("Save and finish".equals(button)){
    // Do a different stuff
}
else {
    // None of them were pressed
}

Or also you can use a different name value for both buttons:

<input type="submit" name="button1" value="Save and continue"/>
<input type="submit" name="button2" value="Save and finish"/>

In your controller:

String button1 = request.getParameter("button1");
String button2 = request.getParameter("button2");

if (button1 != null){
    // Do stuff
}
else if (button2 != null){
    // Do a different stuff
}
else {
    // None of them were pressed
}

Second solution is preferred because it doesn't depend on the value of the elements

Genzotto
  • 1,954
  • 6
  • 26
  • 45
  • This failed to work with springboot. This is what helped me https://stackoverflow.com/questions/42510898/spring-mvc-make-a-button-upon-click-return-a-string-value – Winter MC Feb 28 '21 at 21:59
1

To reduce if-else if you can, and benefit from Spring framework to handle these mapping, try the following solution (if you don't have many meta parameters to send in the form):

<form action="AddToCartListController" method="post">
    <input type="submit" value="Save and continue"/>
</form>

<form action="CommitCartListController" method="post">
    <input type="submit" value="Save and finish"/>
</form>

...

public class CartListController {
    void AddToCartListController(...) {
        // business code
    }
    void CommitCartListController(...) {
        // business code
    }
}

// ... or ...

public class AddToCartListController {
    void actionMethod(...) {
       // business code        
    }
}

public class CommitCartListController {
    void actionMethod(...) {
       // business code
    }
}

and define a proper mapping in Spring configuration.

mohamed sulibi
  • 526
  • 3
  • 12