これはかなり単純なはずですが、2日目は理解できません。定期的なログインフォームを含むlogin.jspがあります。基本的に自分自身に投稿し、フォームが初めてヒットしたかどうか、またはフォームがデータとともに送信されたかどうかに基づいて、コントローラーにアクションを実行させたいと思います。
何が起こっているのかというと、空白のフォームは正常に読み込まれますが、ユーザー名とパスワードを送信すると、HTTP404が返されます。
<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);
}
}