将复选框值从表单传递到Thymeleaf中的Controller

时间:2018-10-21 10:55:23

标签: checkbox thymeleaf

我的问题是我找不到有关如何将表单中的复选框值(选中/未选中)传递给我的@Controller的任何信息。 我将Thymeleaf用于前端。

这是我的代码:

<form action="#" th:action="@{/passwordaenderung}" th:object="${password}" method="post">
    <table border="1" style="width:50%">
        <thead>
            <tr>
                <th> Benutzername </th>
                <th> Passwort </th>
            </tr>
        </thead>
        <tbody>
        <tr th:if="${users.empty}">
                <td colspan="2"> No Users available</td>
            </tr>
            <tr th:each="user : ${users}">
                <td><input type="text" th:value="${user.benutzername}" name="username" id="username"></td>
                <td><input type="text" id="password" name="password" required />
                <td><input type="checkbox" th:field="*{multiCheckboxSelectedValues}" />
            </tr>
        </tbody>
    </table>
    <input type="submit" value="Submit">
</form>

这是我的控制器:

   @PostMapping
    public String updateOldPassword(@Valid @ModelAttribute("password") PasswordChange userAndpassword,
            BindingResult result, ModelMap model) {
    if (result.hasErrors()) {
        return "error";
    }

    String username = userAndpassword.getUsername();
    String password = userAndpassword.getPassword();

    String newPassword = passwordEncoder.encode(password);

    passwordRepo.updatePassword(username, newPassword);
    return "redirect:/";
}

如果可能的话,我想完成将所有字段放入列表的操作,以便在列表中包含用户名,密码和复选框。

1 个答案:

答案 0 :(得分:0)

所以,我能够解决我的问题:

这是我的观点:

<form action="#" th:action="@{/passwordaenderung}" th:object="${form}"
  method="post">
    <fieldset>
        <table>
            <thead>
                <tr>
                    <th> Title</th>
                    <th> Author</th>
                </tr>
            </thead>
            <tbody>
                <tr th:each="book, itemStat : *{users}">
                    <td><input th:field="*{users[__${itemStat.index}__].username}" /></td>
                    <td><input th:field="*{users[__${itemStat.index}__].password}" /></td>
                </tr>
            </tbody>
        </table>
        <input type="submit" id="submitButton" th:value="Speichern">
    </fieldset>
</form>

控制器:

@Controller
@RequestMapping("/passwordaenderung")
public class Passwortaenderung {

    @Autowired
    private UserRepository userRepo;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private updateOldPasswordRepository passwordRepo;

    @GetMapping 
    public String showPasswordChangeForm(Model model) {
        UserCreationDto userForm = new UserCreationDto();
        User user;

        List<Benutzer> b = userRepo.findAll();

        for(int i = 0; i<b.size(); i++) {
            user = new User();
            user.setUsername(b.get(i).getBenutzername());
            user.setPassword(b.get(i).getPasswort());
            user.setChecked(false);
            userForm.addBook(user);
        }

        model.addAttribute("form", userForm);
        return "passwordaenderung";
    }

    @PostMapping
    public String updateOldPassword(@ModelAttribute UserCreationDto userTableSettings,
                                    BindingResult result,
                                    Model model) {
        if (result.hasErrors()) {
            return "error";
        }

        List<User> users = userTableSettings.getUsers();

        for(int i = 0; i<users.size(); i++) {
            String username = users.get(i).getUsername();
            String newPassword = passwordEncoder.encode(users.get(i).getPassword());

            passwordRepo.updatePassword(username, newPassword);             
        }
        return "redirect:/passwordaenderung";
    }
}

我现在要完成的操作也是在表单上放置一个复选框,以便用户可以选择要更新的用户。

但是如何获得带有复选框的行?