I have two models :
public class Size {
private String name;
// getter and setter
}
public class Product {
private int id;
private String designation;
private Float price;
private List<Size> availableSizes;
// Getter and setters
}
I have defined Sizes in my servlet, and now I nead to create products with the availables sizes.
What I do in my index Controller:
ModelAndView render = new ModelAndView("admin/index");
render.addObject("products", productFactory.getProducts());
render.addObject("sizes", sizeFactory.getSizes());
render.addObject("command", p);
return render;
I've a list of products, and my list of sizes.
In my index view, I do:
<form:form method="post" action="/ecommerce/admin/products" class="form-horizontal">
<form:input path="id" />
<form:input path="designation" />
<form:input path="price" />
<form:select path="availableSizes" items="${sizes}"/>
<input type="submit" value="Ajouter le produit" class="btn" />
</form:form>
Then, in new product controller, I do :
// To fix: Sizes not saved !
@RequestMapping(value = "/products", method = RequestMethod.POST)
public ModelAndView newProduct(@ModelAttribute("Product") Product product,
BindingResult result) {
productFactory.add(product);
return buildIndexRender(null, null, product);
}
The problem is that I keet the posted product, but not the corresponding sizes.
Is there a way to keep selected sizes in the form int the controler, or directly in the model?
Thanks.