I'm trying to edit a product. The form backing object is pretty simple:
private Integer productId;
private String name;
private Double price;
private List<Integer> relatedProductList; //list of related product ids
...//getters/setter
The part causing the problem is the relatedProductList. I'm trying to put the list on post to display it on a subsequest page. I tried using a hidden field like this in my jsp:
<form:hidden path="relatedProductList"/>
The hidden field appears nicely in the html as you would expect:
<input id="relatedProductList" name="relatedProductList" type="hidden" value="[200408, 200417]"/>
The post data looks good using firebug:
relatedProductList [200408, 200417]
But in my controller the form backing object has a null product list??
@RequestMapping(method = RequestMethod.POST, value = "/edit.do", params = "editRelatedProducts")
public ModelAndView editRelatedProducts(@Valid @ModelAttribute ProductForm form, BindingResult result) {
if (result.hasErrors()) {
ModelAndView view = new ModelAndView(VIEW_PRODUCT);
setupCreateReferenceData(view , form);
return view ;
}
ModelAndView editView = new ModelAndView(VIEW_EDIT_RELATED);
//method to lookup the product ids and place product objects on model
editView.addObject("relatedProducts",populateProductList(form.getRelatedProductList()));
return editView ;
}
** But the form.getRelatedProductList is null!
I can get around the problem by using a hidden field and setting the value in the jsp, in the loop that shows the related products:
<div>
<table id="relProductTbl" class="tablesorter">
<thead>
...
</thead>
<tbody>
<c:forEach var="prod" items="${relatedProducts}" varStatus="row">
<tr>
<input id="relatedProductList" name="relatedProductList" type="hidden" value="${prod.productId}"/>
...
</tr>
</c:forEach>
</tbody>
</table>
</div>
This produces the following html:
<input id="relatedProductList" name="relatedProductList" type="hidden" value="200408"/>
...
<input id="relatedProductList" name="relatedProductList" type="hidden" value="200417"/>
Which seems fine and produces the following post:
relatedProductList 200408
relatedProductList 200417
And suddenly the form.getRelatedProductList() is now populated correctly.
Does anyone know why the post data contractList [200408, 200417] doesn't get bound to the form correctly when using springs form:hidden tag? Is this a bug or the expected behaviour. Seems very strange to me just wanted to throw it out there and see if I'm doing it wrong perhaps, or if it can help anyone else.
Thanks.