2015-11-16 40 views
0

我想用Spring创建一个简单的登录页面。
我已经创建了一个如教程[这里] [1]
中所述的表单。出于某种原因,当我点击提交按钮时,请求不会通过控制器。 (我在控制器方法的第一行设置了一个断点)
有没有人有一个想法,为什么?提交春季的HTML表单

我的代码:
控制器:

@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> 

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

+0

那么究竟发生了什么?你得到一个404错误或其他东西? –

+0

@AlanHay - 我在URL末尾使用“#”获得相同(干净)视图 – YuriG

回答

0

删除@ModelAttribute,然后再试一次。

+0

为什么要解决它? –

+0

@Sam应该是一个评论 – Raf

+0

@Sam - 它没有解决问题 – YuriG