Thymeleaf动态表单操作不起作用

时间:2019-06-20 16:17:13

标签: spring thymeleaf

我正在通过设置以下模型属性来加载home.html页面:

@GetMapping("/")
private String getHomePage(Model model) {
    model.addAttribute("allUsers", this.userService.findAll());
    model.addAttribute("user", new User());
    model.addAttribute("queryResult", "");
    model.addAttribute("errors", new ArrayList<>());
    return "home";
}

这是我用于更新用户数据的表格:

<form action="#" th:action="@{/user/{username}(username=${user.username})}" th:object="${user}" th:method="put">
    <label for="username">Username</label>
    <select th:field="*{username}" id="username">
        <option th:each="singleUser : ${allUsers}" th:value="${singleUser.username}" th:text="${singleUser.username}"></option>
    </select>

    <label for="password">Password</label>
    <input type="password" th:field="*{password}" id="password" >
    <span th:if="${#fields.hasErrors('password')}" th:errors="*{password}"></span>

    <label for="status">Status</label>
    <select th:field="*{status}" id="status">
        <option th:value="'Activated'" th:text="Activated"></option>
        <option th:value="'Deactivated'" th:text="Deactivated"></option>
    </select>
    <button type="submit">UPDATE DATA</button>
</form>

当我尝试提交此表单时,它没有将username绑定到URL。 而不是/user/whateverUsername,我只有/user我需要绑定此选择元素的值:

<select th:field="*{username}" id="username">
        <option th:each="singleUser : ${allUsers}" th:value="${singleUser.username}" th:text="${singleUser.username}"></option>
</select>

我该如何解决这个问题?

非常感谢您!

0 个答案:

没有答案