You have several submit buttons in the form and determine what was clicked in the controller

In my Spring application, I have jsp that has a form where I want to have multiple submit buttons that go to the same controller. I need to determine which button was pressed in the controller. The form displays several elements to the user, and they can select one of the elements, with the only difference being the identifier of the selected element.

In jsp, I create the form as follows:

<form:form method="post" commandName="myCommand"> <c:forEach items="${model.availableitems}" var="item"> <span class="item">${item.description}</span> <input type="hidden" name="id" value="${item.ID}"/> <input type="submit" name="SelectButton" value="Select" /> </c:forEach> </div> </form:form> 

However, this gives me the message "Data binding errors: 1" in the log, and the form is not submitted to the controller.

I tried changing myCommand.id from int to String, but then the value when it was sent is id1, id2, id3 ... (all identifiers in a comma-separated list) without the ability to determine which button was pressed. I do not want to specify different actions for each button, as the number of elements can grow, and the action is the same for them, just with a different identifier.

How can I use several buttons in this form and get the value in the controller?

+4
source share
4 answers

Why not just use a separate form for each element?

 <c:forEach items="${model.availableitems}" var="item"> <form:form method="post" commandName="myCommand"> <span class="item">${item.description}</span> <input type="hidden" name="id" value="${item.ID}"/> <input type="submit" name="SelectButton" value="Select" /> </form:form> </c:forEach> 
+7
source

instead of the submit button, use a button that calls a function that sets the value of the hidden field and calls form.submit()

you can create buttons with a loop

0
source

You have 3 options -
1. Use several forms and enter the submit button in each form as described above. The generated code will not be very pretty, and it is generally believed that bad practice has several forms on the page.
2. Use the Javascript hack to set the hidden variable when the submit button is clicked.
3. If you have to process the backup (without JavaScript), then there is a roundabout way to find out which button was clicked. Here is an example -

 <form:form method="post" commandName="myCommand"> <c:forEach items="${model.availableitems}" var="item"> <span class="item">${item.description}</span> <input type="submit" name="${item.ID}" value="Select" /> </c:forEach> </form:form> 

This sets a unique identifier for each button. Since query parameters go as pairs of key values, we will have to do a reverse search in this case, since we have a value in the key position. Here's the java side of this. Not sure if this is the most efficient way to do this, but it works.

 String searchButtonName(final HttpRequest request) { String buttonName = ""; Map<String, String[]> paramMap = request.getParameterMap(); if (MapUtils.isNotEmpty(paramMap)) { for (Map.Entry<String, String[]> entry : paramMap.entrySet()) { /* Search for the button name as given in the 'value' attribute for the input tag */ if ("Select".equals(entry.getValue()[0])) { buttonName = entry.getKey(); break; } } } return buttonName; } 
0
source

You can use the @RequestParam("SelectButton") String submit annotation parameter in your controller's argument list.

0
source

All Articles