Spring collection of bindings for hidden field shaping

I am trying to edit a product. The form support 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 related to the corresponding list of products. I am trying to put a list in a message to display it on a subsequence page. I tried using a hidden field like this in jsp:

 <form:hidden path="relatedProductList"/> 

The hidden field renders beautifully in html, as you would expect:

 <input id="relatedProductList" name="relatedProductList" type="hidden" value="[200408, 200417]"/> 

Post data looks good using firebug:

 relatedProductList [200408, 200417] 

But in my controller, the form support object has a zero list of products

 @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 form.getRelatedProductList is null!

I can get around the problem by using a hidden field and setting the value in jsp, in a loop that shows 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 creates the following html:

 <input id="relatedProductList" name="relatedProductList" type="hidden" value="200408"/> ... <input id="relatedProductList" name="relatedProductList" type="hidden" value="200417"/> 

Which seems beautiful and creates the following record:

 relatedProductList 200408 relatedProductList 200417 

And suddenly form.getRelatedProductList () is now correctly populated.

Does anyone know why postcontractList [200408, 200417] does not get form bindings correctly when using the springs form: hidden tag? Whether this is a bug or expected behavior. It seems very strange for me to just want to throw it out there and see if I am doing it wrong, or if it can help someone else.

Thanks.

+4
source share

Source: https://habr.com/ru/post/1385394/


All Articles