spring mvc - How to map List<Item> in POST method RequestMapping -
spring boot 1.5.4 need take list<orderto>
in post method.
controler:
@postmapping("/orderlist") public string confirmorder( , @requestparam(name = "id", required = false) list<integer> idl , @requestparam(name = "selected", required = false) list<integer> selectedl , @requestparam(name = "typename", required = false) list<string> typenamel // ...another lists // ,@modelattribute list<orderto> notwork ) { // collect lists
the best option found lists fields of class. maybe i'm missing something.
view:
<form action="orderlist" method="post" id="catalogform"> <table class="table table-hover table-bordered table-striped table-condensed"> <tr> <th><#--...--></th> </tr> <#list coffeetypelist type> <#--freemarker--> <tr> <td> <input type="hidden" name="id" value="${type.id}"> <input type="checkbox" name="selected" value="${type.id}" > </td> <td><input type="hidden" name="typename" value="${type.typename}">${type.typename}</td> <td><input type="hidden" name="price" value="${type.price}">${type.price} tgr</td> <td><input type="number" size="3" name="quantity" min="0" step="1"/></td> </tr> <#--...--> </#list> <#--...-->
model:
public class orderto{ private int id; private string typename; private int quantity; private double price; private boolean selected; //+constructors +getters + setters
is possible take list without additional collecting operations? how?
thanks shantaram_t direction of thought. key need wrapper class.
public class ordertowraper { @valid @notnull private list<orderto> items;
after that, valid ordertowraper object available controller:
@posttmapping("/orderlist") public string confirmorder(@valid ordertowraper ordertowraper, bindingresult result, modelmap map) { if (result.haserrors()) { //...
there few non-obvious moments list-binding using freerarker ( <@spring.forminput>
not work)
<#list ordertowraper.items type> <tr> <td> <@macros.textinputhidden "ordertowraper.items[${type_index}].id"/> <input type="checkbox" name="items[${type_index}].selected" value="true"> </td> <td><@macros.textinputhidden "ordertowraper.items[${type_index}].typename"/>${type.typename}</td> <td><@macros.textinputhidden "ordertowraper.items[${type_index}].price"/>${type.price}</td> <td><@macros.textinput "ordertowraper.items[${type_index}].quantity" "class='form-control'"/></td> <td><@macros.showerrors "<br>" "text-danger"/></td> </tr> </#list>
and freemarker macro:
<#macro textinputhidden path> <@spring.bind path/> <#assign id="${spring.status.expression?replace('[','')?replace(']','')}"> <input type="hidden" id="${id}" name="${spring.status.expression}" value="${spring.stringstatusvalue}"> <#nested> </#macro>
Comments
Post a Comment