【问题标题】:Spring select multiple tag and bindingSpring选择多个标签和绑定
【发布时间】:2012-05-28 01:12:12
【问题描述】:

我正在尝试使用 spring 的 select 标签来选择多个选项来填充 List。 我的选择标签显示良好,当我选择选项时,列表已正确更新。

我遇到的唯一问题是,当我使用已填充的列表渲染 for 时,我的选择标签不会突出显示选定的选项。我已经尝试调试,我可以看到 List 不是空的,它实际上是似乎没有将所选选项标记为选中的标签。

我的代码:

@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>

【问题讨论】:

    标签: spring jsp spring-mvc


    【解决方案1】:

    这可能是由于所选产品列表与显示产品的完整列表不包含相同的实例。

    标签将产品与equals() 进行比较,并且您尚未在产品类中覆盖equals()(和hashCode())。

    因此,即使所选产品包含名称为“foo”的产品,并且产品的完整列表也包含名称为“foo”的产品,这些产品并不相等,因此 Spring 不知道它们'是同一产品,因此应该选择该产品。

    【讨论】:

    • 太棒了!我在想它是用 itemValue 来比较的!确实,使用 equals 更有意义...
    猜你喜欢
    • 2010-10-16
    • 2010-10-17
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    相关资源
    最近更新 更多