提交Spring的

时间:2015-11-16 11:56:24

标签: java html spring forms spring-mvc

我正在尝试使用Spring创建一个简单的登录页面 我已经创建了一个表格,如教程[here] [1]中所述 出于某种原因,当我点击提交按钮时,请求不会通过控制器。 (我在控制器方法的第一行设置了一个断点)
有谁知道为什么?

我的代码:
控制器:

@RequestMapping(value="/login", method=RequestMethod.POST)
public String login(@ModelAttribute(value="loginModel") LoginModel loginModel) {
    if(loginModel.getEmail().equals("null") || loginModel.getPassword().equals("null")) {
        return "Wrong user email or password";
    }
    return "result";
}

型号:

public class LoginModel {
    private String email;
    private String password;
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public LoginModel(String email, String password) {
        super();
        this.email = email;
        this.password = password;
    }
}

查看:

    <!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" href="css/login.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    </head>
    <body>
        <div class="form">     
          <ul class="tab-group">
            <li class="tab active"><a href="signup.html">Sign Up</a></li>
            <li class="tab"><a href="login.html">Log In</a></li>
          </ul>
          <div id="login">   
            <h1>Welcome Back!</h1>          
            <form action="#" th:action="@{/login}" th:object="${loginModel}" method="post">        
              <div class="field-wrap">
                <input type="email" required autocomplete="off" th:field="*{email}"/>
              </div>          
              <div class="field-wrap">
                <input type="password" required autocomplete="off" th:field="*{password}"/>
              </div>          
              <p class="forgot">
                <a href="#">Forgot Password?</a>
              </p>         
              <input type="submit" value="Log in" />       
            </form>
          </div>      
        </div>
    </body>
</html>

更新:
发现问题...
LoginModel类必须有空构造函数

1 个答案:

答案 0 :(得分:0)

删除@ModelAttribute,然后重试。