這個應該很簡單,但是第2天我弄不明白。我有一個包含例程登錄表單的login.jsp。我想基本上發佈自己,並讓控制器根據表單是第一次被觸發還是與數據一起提交來採取行動。春天3:不能處理表格
發生了什麼事是,空白表格加載罰款,但在提交用戶名和密碼的,我得到一個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支持對象:
package 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);
}
}
表單操作是/ do/login,映射是/ login。 /還有其他映射嗎?也許在類或web.xml級別? –
是啊有一個servlet映射的web.xml中的彈簧調度器爲/ do – Lurk21
是映射/ *還是/*.jsp? –