I'm trying to validate a form using Spring MVC, and it's working, but when the view containing the error messages comes, the fields I create through a forEach using an object I send from the controller aren't being created.
form.jsp
<form:form action="/project/product" method="POST" commandName="product">
<div>
<label>Title</label>
<input type="text" name="title">
<form:errors path="title"></form:errors>
</div>
<div>
<label>Description</label>
<textarea rows="10" cols="20" name="Description"></textarea>
<form:errors path="Description"></form:errors>
</div>
<c:forEach items="${types}" var="priceType" varStatus="status">
<div>
<label>${priceType.nome}</label>
<input type="text" name="prices[${status.index}].value">
<input type="hidden" name="prices[${status.index}].type" value=${priceType }>
</div>
</c:forEach>
<button type="submit">Save</button>
</form:form>
Controller:
@RequestMapping("/form")
public ModelAndView form(ModelMap m) {
m.addAttribute("product", new Product());
ModelAndView modelAndView = new ModelAndView("products/form");
modelAndView.addObject("types",PriceType.values());
return modelAndView;
}
@RequestMapping(method=RequestMethod.POST)
public String grava(@Valid Product product, BindingResult result, RedirectAttributes redirectAtributes, ModelMap m) {
if(result.hasErrors()) {
return "products/form";
}
productDao.save(product);
return "redirect:products/list";
}
I've tried to return ModelAndView instead of String, no success, tried using "redirect:", but then even the validation messages don't work.