这应该很简单,但是第 2 天我无法弄清楚。我有一个 login.jsp,其中包含一个常规登录表单。我想基本上发布给自己,并让控制器根据表单是第一次被击中还是与数据一起提交来采取行动。
发生的情况是空白表单加载正常,但在提交用户名和密码后,我得到一个 HTTP 404。
<div id="messageBox">${loginMessage}</div>
<form id="form1" name="form1" method="post" action="/do/login" onsubmit="return validateForm()">
<table>
<tr>
<td>Username:</td>
<td><input name="username" type="text" size=30 value="" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="password" type="password" size=30 value="" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Login" /></td>
</tr>
</table>
</form>
这些是我的控制器映射:
@RequestMapping(value = "/login", method=RequestMethod.POST)
public ModelAndView login(@RequestParam("username") String username, @RequestParam("password") String password, Model model) {
if (username == null || password == null) {
// User has not specified all required fields
String loginMessage = "Please complete all fields";
return new ModelAndView("login", "loginMessage", loginMessage);
} else {
// User has specified username and password
// Attempt authentication
Login login = new Login();
isAuthenticated = login.authenticate(username, password);
if (isAuthenticated) {
// Authentication succeeded, return the options page
return viewOptions(model);
} else {
// Authentication failed, return the login page
String loginMessage = "Authentication failed";
return new ModelAndView("login", "loginMessage", loginMessage);
}
}
}
@RequestMapping(value = "/login", method=RequestMethod.GET)
public ModelAndView login(Model model) {
// Blank login screen
String loginMessage = " ";
return new ModelAndView("login", "loginMessage", loginMessage);
}
在对此进行大量冲击后进行编辑......我也尝试了以下方法,得到了相同的结果......
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<div id="messageBox">${loginMessage}</div>
<form:form modelAttribute="loginForm" id="form1" name="form1" method="post" action="/do/authenticate" onsubmit="return validateForm()">
<table>
<tr>
<td><form:label path="username">Username:</form:label></td>
<td><form:input path="username" /></td>
</tr>
<tr>
<td><form:label path="password">Password:</form:label></td>
<td><form:input path="password" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Login" /></td>
</tr>
</table>
</form:form>
使用 LoginForm 支持对象:
包 com.cloudfordev.spring3;
public class LoginForm {
private String username;
private String 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;
}
}
以及以下控制器:
@Controller
@SessionAttributes
public class VMGeneratorController {
@ModelAttribute("loginForm")
public LoginForm getLoginFormObject() {
return new LoginForm();
}
@RequestMapping(value = "/viewoptions", method = RequestMethod.GET)
public ModelAndView viewOptions(Model model) {
Menu menu = new Menu();
String optionsPage = menu.draw();
return new ModelAndView("options", "body", optionsPage);
}
@RequestMapping(value = "/authenticate", method = RequestMethod.POST)
public ModelAndView login(@ModelAttribute("loginForm") LoginForm loginForm, BindingResult result) {
boolean isAuthenticated = false;
String username = loginForm.getUsername();
String password = loginForm.getPassword();
if (username == null || password == null) {
// User has not specified all required fields
String loginMessage = "Please complete all fields";
return new ModelAndView("login", "loginMessage", loginMessage);
} else {
// User has specified username and password
// Attempt authentication
Login login = new Login();
isAuthenticated = login.authenticate(username, password);
if (isAuthenticated) {
// Authentication succeeded, return the options page
String loginMessage = "Success";
return new ModelAndView("login", "loginMessage", loginMessage);
} else {
// Authentication failed, return the login page
String loginMessage = "Authentication failed";
return new ModelAndView("login", "loginMessage", loginMessage);
}
}
}
@RequestMapping(value = "/login", method=RequestMethod.GET)
public ModelAndView login(Model model) {
// Blank login screen
String loginMessage = " ";
return new ModelAndView("login", "loginMessage", loginMessage);
}
}