繁体   English   中英

在 Thymeleaf 中迭代二维数组时如何访问字段?

[英]How to access to field when iterating over 2d array in Thymeleaf?

目前我正在学习 Thymeleaf。 我想向用户展示包含按钮和值的简单网格。 问题是在迭代期间我无法访问 Java object 的变量。 二维数组由 SeatsSeances 对象组成。

@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class SeatsSeance {
    private Integer id;
    private Integer seatId;
    private Integer seanceId;
    private SeatState state;
}
<table>
<tr th:each="row: ${seatSeances}">
<td th:each="place: ${row}">

<input type="button" th:text="${place.state.name()}"> //not working
<input type="button" th:text="${place.getState().name()}"> //not working

</td>
</tr>
</table>

当我只是尝试th:text="${place.state.name()}"时出现错误:

Thu Aug 20 09:38:54 CEST 2020
There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource [templates/buyticket.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/buyticket.html]")

我做错了什么? <td th:each>标签里面应该有什么?

更新 1

SeatSeance class:

@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class SeatsSeance {
    private Integer id;
    private Integer seatId;
    private Integer seanceId;
    private SeatState state;
} 

座椅状态 class

public enum SeatState { RESERVED, ORDERED, FREE
}

IDE 允许我使用 HTML 中的所有字段,因此问题不在于可访问性。 更重要的是,使用没有问题

<input type="button" th:text="${place}">

当我想显示 object 的一个字段时,问题就开始了。 尽管 getter 和 setter 我做不到

更新 2

在此处输入图像描述

在此处输入图像描述

您必须为 class 的所有属性提供 getter/setter。 如果使用 Spring Lombok,则需要添加@Getter @Setter标签。

暂无
暂无

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

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