问题是我想使用对象AdminLogin来验证管理员。
这是AdminLogin类
package com.mintad.admin.beans;
import java.io.Serializable;
import org.springframework.stereotype.Component;
/**
*
* Used to authenticate admin
*
*/
@Component("adminLogin")
public class AdminLogin implements Serializable {
private static final long serialVersionUID = -6394045014528037520L;
private String username;
private String password;
public AdminLogin() {
}
public AdminLogin(String username, String password) {
super();
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
和控制器方法
@ModelAttribute("adminLogin")
public AdminLogin createModel() {
return new AdminLogin();
}
@RequestMapping(value = "/admin/login", method = RequestMethod.POST)
public ModelAndView login(@ModelAttribute AdminLogin adminLogin, HttpServletRequest request,
@RequestParam(value = "error", required = false) String error, @RequestParam(value = "logout", required = false) String logout) {
LOGGER.debug("admin login page");
ModelAndView model = new ModelAndView();
model.addObject("adminLogin", adminLogin);
if (error != null) {
model.addObject("error", "Invalid username and password!");
}
if (logout != null) {
model.addObject("msg", "You've been logged out successfully.");
}
model.setViewName("/admin/index");
LOGGER.debug("returning admin login page");
return model;
}
@RequestMapping(value = "/admin/login", method = RequestMethod.GET)
public String greetingForm(Model model) {
model.addAttribute("adminLogin", new AdminLogin());
return "/admin/login";
}
问题是表格是通过GET提交的,尽管它的POST如下;
<form class="m-t" role="form" th:action="@{/admin/login}" th:object="${adminLogin}" method=" POST" autocomplete="off">
<div th:if="${param.error}" class="alert alert-danger">Invalid username and password.</div>
<div th:if="${param.logout}" class="alert alert-success">You have been logged out.</div>
<div class="form-group">
<input type="text" class="form-control" id="username" name="username" th:field="*{username}" placeholder="Username" required="" />
</div>
<div class="form-group">
<input type="password" class="form-control" id="password" name="password" th:field="*{password}" placeholder="Password" required="" />
</div>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<button type="submit" class="btn btn-primary block full-width m-b">Login</button>
<a href="#"><small>Forgot password?</small></a>
<p class="text-muted text-center">
<small>Do not have an account?</small>
</p>
<a class="btn btn-sm btn-white btn-block" href="register.html">Create an account</a>
</form>
我已经添加了GET以确认我的感觉,也因为我已经看过有关如何在登录表单中使用对象的示例。
请帮我解决这个问题,或者请你更清洁地实现我的目标。
谢谢!