Spring选择多个标签并绑定 [英] Spring select multiple tag and binding
问题描述
我正在尝试使用spring的select标签选择多个选项来填充列表. 我的选择标签显示良好,并且当我选择选项时,列表已正确更新.
I am trying to use the select tag of spring to select multiple options to fill a List. My select tags is well displayed and when I select options the List is correctly updated.
我唯一的问题是,当使用已填充的列表渲染for时,我的select标记不会突出显示所选的选项.我尝试调试,发现列表不是空的,它实际上是标记,似乎没有将所选选项标记为已选中.
The only problem I have is when I render the for with an already filled List, my select tag does not highlight the selected options. I have try to debug and Ican see that the List is not empty, it is really the tag that seems to not mark the selected options as selected.
我的代码:
@Entity
public class ProductsGroup
{
@Version @Column(name = "version")
private Integer version;
@Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id")
private Integer id;
@ManyToMany(fetch = FetchType.EAGER)
private List<Product> products;
public List<Product> getProducts()
{
return products;
}
public void setProducts(List<Product> products)
{
this.products = products;
}
}
@Entity
public class Product
{
@Version @Column(name = "version")
private Integer version;
@Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id")
private Long id;
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
<form:form action="${action}" class="fancyform" commandName="productsGroup" id="productForm">
....
<form:select path="products" items="${products}" itemLabel="name" itemValue="id" multiple="true"/>
....
</form:form>
推荐答案
这可能是由于所选产品的列表不包含与所显示产品的完整列表相同的实例.
It's probably due to the fact that the list of selected products doesn't contain the same instances as the complete list of displayed products.
标签将产品与equals()
进行比较,并且您没有在Product类中覆盖equals()
(和hashCode()
).
The tag compares products with equals()
, and you have not overridden equals()
(and hashCode()
) in your Product class.
因此,即使选定的产品包含名称为"foo"的产品,并且产品的完整列表也包含名称为"foo"的产品,这些产品也不相等,因此Spring并不知道它们是同一产品,因此应选择该产品.
So even if the selected products contain the Product with the name "foo", and the complete list of Product also contains a Product with the name "foo", those products are not equal, and Spring thus doesn't know they're the same product, and that this product should thus be selected.
这篇关于Spring选择多个标签并绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!