【问题标题】:Form value is null with Thymeleaf and Spring MVCThymeleaf 和 Spring MVC 的表单值为 null
【发布时间】:2013-12-31 06:49:31
【问题描述】:

我正在尝试使用 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

如果我尝试使用表单设置名字,则名字完全可以包含在 ReservationSelectionForm 中。

当我传递整个 Reservation 对象 (th:value="${reservation}") 时,ReservationSelectionForm 中始终是 null

有人可以向我解释和/或帮助我传递整个预订对象吗?

【问题讨论】:

    标签: java forms spring-mvc thymeleaf


    【解决方案1】:

    您的选择列表始终为空。
    &lt;option th:each="reservation : ${reservationlist}"
    reservationlist 从未添加到模型中,因此 th:each 正在迭代 null

    【讨论】:

      猜你喜欢
      • 2015-07-05
      • 1970-01-01
      • 2023-03-25
      • 2018-06-09
      • 2016-03-14
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多