Spring从页面获取模型属性

时间:2014-07-25 09:43:35

标签: spring spring-mvc thymeleaf

我从页面到控制器获取参数时出现问题。它是一个复选框和字段' rememberMe'但是它的值始终为false(或者是true,当我将rememberMe设置为true时)。

控制器:

@RequestMapping("/login")
@Controller
public class LoginController {

    private UsersService userService;
    private boolean rememberMe;

    @Autowired
    public LoginController(UsersService usersRepo) {
            this.userService = usersRepo;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String initCreationForm(Model model) {
            model.addAttribute("user", new Users());
            model.addAttribute("rememerMe", rememberMe);
            return "login";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String login(Model model, @ModelAttribute("user") Users user, BindingResult result, SessionStatus status,
                    HttpServletResponse response, HttpServletRequest request) {
            LoginValidator loginValidator = new LoginValidator(userService);
            loginValidator.validate(user, result);
            System.out.println(rememberMe);
            if (result.hasErrors()) {
                    System.out.println("Error registerController");
                    return "login";
            } else {
                    userService.login(user, rememberMe, request, response);
                    status.setComplete();
                    // return "redirect:/owners/" + user.getId();
                    return "redirect:/";
            }

    }

    public boolean isRememberMe() {
            return rememberMe;
    }

    public void setRememberMe(boolean rememberMe) {
            this.rememberMe = rememberMe;
    }

}

页:

<!DOCTYPE html> 
<html lang="en">  
<head th:replace="fragments/headTag :: headTag" /> 
<body>
    <div class="container">
            <div th:replace="fragments/bodyHeader :: bodyHeader"></div>


            <h2 th:text="#{login.header}" />
            <span th:text="#{login.info}" />
            <form th:object="${user}" th:method="post" action="./login.html" th:action="@{${#httpServletRequest.servletPath}}"
                    class="form-horizontal" id="login-form">
                    <div class="control-group" th:classappend="${#fields.hasErrors('username')} ? error">
                            <label class="control-label" th:text="#{login.username}" />
                            <div class="controls">
                                    <input type="text" th:field="*{username}" /> <span class="help-inline" th:errors="*{username}">[error]</span>
                            </div>
                    </div>

                    <div class="control-group" th:classappend="${#fields.hasErrors('password')} ? error">
                            <label class="control-label" th:text="#{login.password}" />
                            <div class="controls">
                                    <input type="password" th:field="*{password}" /> <span class="help-inline" th:errors="*{password}">[error]</span>
                            </div>
                    </div>

                    <div class="control-group" >
                    <label class="control-label" th:text="#{login.rememberme}" />
                            <input type="checkbox" th:value="${rememberMe}" />
                    </div>

                    <div class="form-actions">
                            <button type="submit" class="btn btn-primary" th:text="#{login}" />
                    </div>
            </form>
    </div>

    <div th:replace="fragments/footer :: footer"></div> 
</body> 
</html>

我也试过

<input type="checkbox" th:field="*{rememberMe}" />

但是Spring提出了一个错误,即用户类没有财产,请记住我&#39;

0 个答案:

没有答案