【问题标题】:java spring thymeleaf: bind object with enum-list-attribute attribute to html formjava spring thymeleaf:将具有 enum-list-attribute 属性的对象绑定到 html 表单
【发布时间】:2015-02-02 13:07:01
【问题描述】:

我有一个包含可能运输选项列表的实体:

//ShippingType.java
public enum ShippingType {
    DOWNLOAD,
    SHIPPING
}


//SoftwareType.java
@Entity
public class SoftwareType {

    //...
    @Column(unique = true)
    private String name;

    @ElementCollection(targetClass=ShippingType.class)
    @CollectionTable(name = "softwaretype_shippingtype", joinColumns = @JoinColumn(name = "softwaretype_id"))
    @Column(name = "shippingtype", nullable = false)
    @Enumerated(EnumType.STRING)
    private List<ShippingType> supportedShippingTypes;

    //...

    public List<ShippingType> getSupportedShippingTypes() {
        return supportedShippingTypes;
    }

    public void setSupportedShippingTypes(List<ShippingType> supportedShippingTypes) {
        this.supportedShippingTypes = supportedShippingTypes;
    }
}

现在我想将带有 thymeleaf 的对象绑定到 html-form 以轻松创建/编辑此类实体。

<div class="panel-body" th:fragment="createUpdateForm(action, SoftwareType)">
    <form role="form" action="#" th:action="@{${action}}" th:object="${SoftwareType}" method="post">

        <label for="softwareTypeName">Name</label>
        <input type="text" th:field="*{name}" id="softwareTypeName"/>

        <!-- how to insert checkboxes of shippingTypes ?? -->
        <button type="submit" value="submit"/>
    </form>
</div>

但是如何插入包含所有 ShippingTypes 的复选框列表并将其绑定到对象?

【问题讨论】:

  • 你的控制器是什么样子的?
  • 我认为您的请求映射方法中需要一个列表

标签: spring enums thymeleaf


【解决方案1】:

您可以遍历您的枚举 ShippingType,例如:

<div th:each="shippingType : ${T(com.example.ShippingType).values()}">
    <input type="checkbox" th:id="${{shippingType}}" th:value="${{shippingType}}" th:field="*{supportedShippingTypes}" />
    <label th:for="${{shippingType}}">Shipping Types</label>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-13
    • 2017-04-08
    • 2011-05-28
    • 2015-12-06
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 2015-02-01
    相关资源
    最近更新 更多