简体   繁体   English

将spring表单绑定到list元素

[英]Bind spring form to a list element

Is it possible to bind spring form:form to a list element? 是否可以将spring form:form绑定到列表元素? 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. 在弹簧控制器中填充products列表的位置。

@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 . 接收到: 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. 据我了解,这意味着spring还没有找到将表格绑定的位置。

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. 传递给commandName属性的值是一个键,并且无法像正常的EL或SpEL表达式那样解析它。 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. Spring会寻找一个找不到的名为products[0]的模型属性。

The alternative is to put the first element of the list in the model directly with a key you will use in your jsp. 另一种方法是使用将在jsp中使用的键将列表的第一个元素直接放在模型中。

Or you can use JSTL, get the first element in the list and create an HTML <form> element yourself. 或者,您可以使用JSTL,获取列表中的第一个元素,然后自己创建一个HTML <form>元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM