Form Select和Thymeleaf

时间:2017-04-26 08:24:34

标签: spring thymeleaf

我正在将我的应用程序移植到Thymeleaf,我在创建从对象列表中选择表单方面遇到了问题。

我有这个表格

    <form th:with="reservationUrl=(${price != null} ? '/book/confirm' : '/book/new')"
th:action="@{${reservationUrl}}" method="POST"
class="form-signin" th:object="${reservation}"
id="form-signin">

    <div class="form-group row">
            <label class="col-sm-2 col-form-label" for="plate"
            th:text="#{reservation.plate }">Targa</label>
    <div class="col-sm-4 input-group">
    <select th:type="*{plate}" class="form-control">
            <option th:each="p : ${plates}"
    th:value="${p.plateId}" th:text="${p.plateNumber}">Opzione</option>
            </select>
                </div>
// rest of the form

从控制器我有一个List<Plate> plates而我的Reservation有一个Plate字段,所以它是一个非常基本的选择关联(使用普通的JSP)

我收到此异常

Exception evaluating SpringEL expression: "p.plateId" (reservation:67)] with root cause 
Property or field 'plateId' cannot be found on object of type 'org.thymeleaf.util.EvaluationUtil$MapEntry' - maybe not public?

这是Plate

@Entity
public class Plate {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)

    private Long plateId;

    private String plateNumber;

    private String plateName;

    @ManyToOne
    @JoinColumn(name="userId")
    @JsonBackReference(value="plates-user-reference")
    private User user;

    private boolean favourite;

    @JsonIgnore
    private boolean enabled;
    //standard getters and setters

1 个答案:

答案 0 :(得分:0)

示例

 <div class="form-group">
                    <label for="location">Pick a location:</label>
                    <select class="form-control" th:value="${appointment.location}" name="location" id="location"
                            >
                        <option disabled="disabled" selected="selected" > -- select the location --</option>
                        <option>Boston</option>
                        <option>New York</option>
                        <option>Chicago</option>
                        <option>San Francisco</option>
                    </select>
                </div>

如果您想尝试下拉菜单,那么您可以按照这种方式进行操作。