Is it possible to bind spring form:form
to a list element? I've tried this way.
<form:form commandName="products[0]">
<form:input path="name"/>
</form:form>
also
<form:form commandName="products0">
<form:input path="name"/>
</form:form>
Where products
list is populated in spring controller.
@RequestMapping(method = RequestMethod.GET)
public String getAll(Map<String, Object> map) {
map.put("products", productService.getAll());
return "products";
}
Received: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'products[0]' available as request attribute
. Which as I understand means that spring haven't found where to bind the form.
No, this is not possible. The value you pass to the commandName
attribute is a key and it is not resolved like a normal EL or SpEL expression would be. It is used directly. In other words, with
<form:form commandName="products[0]">
<form:input path="name"/>
</form:form>
Spring will look for a model attribute called products[0]
which it won't find.
The alternative is to put the first element of the list in the model directly with a key you will use in your jsp.
Or you can use JSTL, get the first element in the list and create an HTML <form>
element yourself.