【发布时间】:2021-02-09 11:13:38
【问题描述】:
我正在尝试在 Thymeleaf 中使用 可为空的布尔值。如果我使用普通的布尔值(原始类型),一切正常。但是当我使用 Boolean 类 时,会出现以下错误:
SmokingAllowed 不可读或具有无效的 getter 方法:是否 getter 的返回类型是否与 setter 的参数类型匹配?
下面的代码应该让您清楚地了解我想要实现的目标。
RoomFilter(Spring 类)
public class RoomFilter {
private RoomType roomType;
private Boolean smokingAllowed;
public RoomType getRoomType() {
return roomType;
}
public void setRoomType(RoomType roomType) {
this.roomType = roomType;
}
public Boolean isSmokingAllowed() {
return smokingAllowed;
}
public void setSmokingAllowed(Boolean smokingAllowed) {
this.smokingAllowed = smokingAllowed;
}
}
HTML (Thymeleaf)
<select class="form-control" th:field="*{smokingAllowed}">
<option th:value="null" selected>---</option>
<option th:value="1">Smoking allowed</option>
<option th:value="0">Smoking not allowed</option>
</select>
【问题讨论】:
标签: spring spring-boot thymeleaf