Java Spring 3:无法处理表单
这应该很简单,但第二天我想不出来。我有一个login.jsp,它包含一个常规登录表单。我想本质上发布到它自己,并让控制器根据表单是否是第一次被点击或者是否与数据一起提交来采取行动Java Spring 3:无法处理表单,java,spring,spring-mvc,Java,Spring,Spring Mvc,这应该很简单,但第二天我想不出来。我有一个login.jsp,它包含一个常规登录表单。我想本质上发布到它自己,并让控制器根据表单是否是第一次被点击或者是否与数据一起提交来采取行动 发生的是空白表单加载良好,但在提交用户名和密码时,我得到一个HTTP 404。p> <div id="messageBox">${loginMessage}</div> <form id="form1" name="form1" method="post" action="/do/log
发生的是空白表单加载良好,但在提交用户名和密码时,我得到一个HTTP 404。p>
<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>
编辑后,对这个沉重的打击。。。
我还尝试了以下方法,得到了相同的结果
<%@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>
以及以下控制器:
@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);
}
}
也许您需要考虑web应用程序的根上下文。假设你的webapp被放置在上,那么你需要发布到/myapp/do/login,而不仅仅是/do/login
如果以上不是解决方案,那么您需要跟踪您的spring context.xml、web.xml、app server specifix config(tomcat context.xml/jboss web.xml)等,以确保所有管道都准备就绪表单操作为/do/login,映射为/login。是否有/do的其他映射?也许在类或web.xml级别?是的,在web.xml中有一个spring调度器的servlet映射,用于/doIs it mapping/*或/*.jsp?/do/*如果您输入的密码不是“hi”,那么您得到404了吗?如果您取回表单,控件将进入
登录
方法,您需要查看主菜单
。其他/do/任何映射正在工作,而不是表单后期映射:(你是对的,我错了。其他/do/映射只有在我手动通过web应用上下文根目录时才起作用。我已将我的应用的上下文根目录更正为基目录,现在一切正常。谢谢!
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);
}
}