我在 thymeleaf 中使用 enum 選擇選項,但我看不到它們或將它們插入到資料庫中。下拉串列中沒有任何內容。

<div class="form-group">
<label for="roomType">Rooms</label>
<select class="form-select selectpicker show-tick" th:field="*{property.roomType}" id="rooms">
<option value="">Nothing selected</option>
<option th:each="property.roomType : ${T(com.realestate.petfriendly.entity.RoomType).values()}"
th:value="${property.roomType}"
th:text="${property.roomType}">
</option>
</select>
</div>
班級
@Enumerated(EnumType.STRING)
@Column(name = "RoomType")
private RoomType roomType;
和列舉
public enum RoomType {
GARSONJERA("garsonjera"), JEDNOIPOSOBAN("jednoiposoban"), DVOSOBAN("dvosoban"),
DVOIPOSOBAN("dvoiposoban"), TROSOBAN("trosoban"), TROIPOSOBAN("troiposoban"),
CERVOROSOBAN("cetvorosoban"), CETVOROIPOSOBAN("cetvoroiposoban"), VISESOBAN("visesoban");
private String roomType;
private RoomType(String roomType) {
this.roomType = roomType;
}
public String getRoomType() {
return this.roomType;
}
}
我不確定為什么它不顯示任何內容
uj5u.com熱心網友回復:
這對我來說是可用的。
班級
@Setter
@Getter
public class Demo {
private RoomType roomType;
}
列舉
public enum RoomType {
GARSONJERA("garsonjera"), JEDNOIPOSOBAN("jednoiposoban"), DVOSOBAN("dvosoban"),
DVOIPOSOBAN("dvoiposoban"), TROSOBAN("trosoban"), TROIPOSOBAN("troiposoban"),
CERVOROSOBAN("cetvorosoban"), CETVOROIPOSOBAN("cetvoroiposoban"), VISESOBAN("visesoban");
private final String roomType;
private RoomType(String roomType) {
this.roomType = roomType;
}
public String getRoomType() {
return this.roomType;
}
}
控制器:
@Controller
@RequestMapping( "test")
public class TestController {
@GetMapping("demo")
public String demo(Model model){
Demo demo = new Demo();
demo.setRoomType(RoomType.CERVOROSOBAN);
model.addAttribute(demo);
return "test/demo";
}
}
HTML:
<div class="form-group">
<label for="rooms">Rooms</label>
<select class="form-select selectpicker show-tick" th:field="*{demo.roomType}" id="rooms">
<option value="">Nothing selected</option>
<option th:each="value : ${T(com.bluray.boot.business.test.RoomType).values()}"
th:value="${value}"
th:text="${value}">
</option>
</select>
</div>
我使用 spring-boot 2.3.6.release
uj5u.com熱心網友回復:
問題就在這里
<option th:each="!!!!property.roomType!!!!<---- : ${T(com.realestate.petfriendly.entity.RoomType).values()}"
th:value="${property.roomType}"
th:text="${property.roomType}">
</option>
雖然應該是
<option th:each="property : ${T(com.realestate.petfriendly.entity.RoomType).values()}"
th:value="${property.roomType}"
th:text="${property.roomType}">
</option>
您遍歷串列并獲取每個元素。在each迭代期間您不會訪問該屬性的嵌套元素。您稍后在通過以下方式訪問該屬性時已執行此操作th:value="${property.roomType}"
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/402211.html
