我正在尝试使用Thymeleaf和Spring MVC处理表单。 用户应选择一个预约,然后保存在表格中。
模板看起来像这样:
<form action="#" th:object="${reservationselectionform}">
<select th:field="*{reservationSelection}" class="selectpicker" data-live-search="true" style="display: none;" >
<option th:each="reservation : ${reservationlist}"
th:value="${reservation}"
th:text="${reservation.user.userAccount.firstname}">Tyler Durden</option>
</select>
<button type="submit" class="btn btn-primary" formaction="/checkintest" formmethod="post">Weiter</button>
我想保存预订对象以在另一个视图中使用它(checkin_test)。这不起作用,因为保留对象始终为空。
控制器如下所示:
@RequestMapping(value="choose_reservation", method = RequestMethod.GET)
public String chose_reservation(ModelMap modelMap{
modelMap.addAttribute("reservationselectionform", new ReservationSelectionForm());
return "checkin/choose_reservation";
}
@RequestMapping("/checkintest")
public String personaldata (ModelMap modelMap,
@ModelAttribute("reservationselectionform") ReservationSelectionForm reservationSelectionForm){
return "checkin/checkintest";
}
表单简单如下:
public class ReservationSelectionForm {
private Reservation reservationSelection;
@NotEmpty
private String firstname;
@NotEmpty
private String lastname;
//getter & setter
如果我尝试使用表单设置firstname,则firstSeditionForm中包含firstname完全正常。
当我传递整个Reservation对象(th:value="${reservation}")
时,它在ReservationSelectionForm中总是null
。
有人可以向我解释这个和/或帮我传递整个预订对象吗?
答案 0 :(得分:0)
您的选择列表始终为空。
<option th:each="reservation : ${reservationlist}"
预订列表永远不会添加到模型中,因此th:each
正在迭代空