Search code examples
spring-mvcjstlspring-roo

Spring Roo Single Form Multiple Submit valued by Id


I am using JQuery dialog to display a <form:form> to identify/select a particular record to be added from query. Every record in the table should contain a button as the submit trigger of the said form to add the selected record. The code below illustrate the structure of the code:

<form:form action="POST" action="form_action" modelAttribute="form">
    <div><form:hidden path="selectedDate" />
         <!-- Other components relating to the parent record -->
    </div>
    <div>
        <!-- The detail of the parent -->
        <table>
            <thead>
                <th>Name</th>
                <th></th>
            </thead>
            <tbody>
                <c:forEach items="${items}" var="itr">
                    <tr>
                        <td>${itr.name}</td>
                        <td>
                            <input type="submit" value="Select" name="${itr.id}" />
                        </td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>
    </div>
</form:form>

Is there a way to get a post parameter of id to indicate the selected record?


Solution

  • You could set the id and name of your submit button to be the proper Spring MVC input name, and make the value the proper selected ID value. Something like this:

    <form:form action="POST" action="form_action" modelAttribute="form">
        <div><form:hidden path="selectedDate" />
             <!-- Other components relating to the parent record -->
        </div>
        <div>
            <!-- The detail of the parent -->
            <table>
                <thead>
                    <th>Name</th>
                    <th></th>
                </thead>
                <tbody>
                    <c:forEach items="${items}" var="itr" varStat="itrStat">
                        <tr>
                            <td>${itr.name}</td>
                            <td>
                                <input type="submit" value="${itr.id}" name="form.items[${itrStat.index}]" id="form.items${itrStat.index}" />
                            </td>
                        </tr>
                    </c:forEach>
                </tbody>
            </table>
        </div>
    </form:form>
    

    All Spring cares about is that the name posted in the Form matches some property on your Model Attribute defined in the Form tags and in the corresponding handler in your Controller.