这可能是一个非常简单的错误。请帮忙。
我有一个带有两个<form:form>
标签的home.jsp页面:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/META-INF/c.tld" prefix="c"%>
<%@ taglib uri="/META-INF/fmt.tld" prefix="fmt"%>
<%@ taglib uri="/META-INF/spring-form.tld" prefix="form"%>
<html>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<head>
<title>Home Page</title>
</head>
<body>
<div>
<form:form name="loginForm" modelAttribute="loginUser" action="login" method="post">
<c:if test="${loginMsg != null}"><c:out value="${loginMsg}"></c:out></c:if>
<br/>
Email: <form:input name="loginEmail" id="loginEmail" value="" path="email"/>
<form:errors path="email" cssClass="error" />
<br/>
password: <form:password name="loginPassword" Id="loginPassword" value="" path="password" />
<form:errors path="password" />
<br/>
<input type="submit" id="id_login" value="Login">
</form:form>
</div>
<div>
<form:form name="SignUpForm" modelAttribute="signUpUser" action="signup" method="post">
<c:if test="${signupMsg != null}"><c:out value="${signupMsg}"></c:out></c:if>
<br/>
Full Name: <form:input name="name" id="name" value="" path="name"/>
<form:errors path="name" cssClass="error" />
<br/>
Email: <form:input name="signupEmail" id="signupEmail" value="" path="email"/>
<form:errors path="email" cssClass="error" />
<br/>
password: <form:password name="signUpPassword" Id="signUpPassword" value="" path="password" />
<form:errors path="password" />
<br/>
<input type="submit" id="id_signUp" value="Sign Up">
</form:form>
</div>
</body>
</html>
每个表单由不同的控制器处理。问题是,当我填写(仅)其中一个表单并单击提交按钮时,ModelAttribute
的两个表格都填充了相同的值
我的控制器没什么特别的:
@RequestMapping("/login")
public String login(@ModelAttribute("loginUser") User user,
BindingResult result, @ModelAttribute("signUpUser") User signUpUser,
BindingResult signUpResult, ModelMap model, HttpServletRequest request,
HttpServletResponse response) {
// Here, both user and signUpUser have the same value (WHY?)
// But I dint fill the sign up form at all
loginFormValidator.validate(user, result);
if(Errors in `result`)
return "forward:/home";
// Authentication Logic
request.getSession().setAttribute("s_user_obj", some_variable);
return "forward:/home";
}
更新
这是生成的HTML。
在login
行动之前:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<head>
<title>Welcome to m0m0</title>
</head>
<body>
<div>
<form id="loginUser" name="loginForm" action="login" method="post">
<br/>
Email: <input id="loginEmail" name="email" name="loginEmail" type="text" value=""/>
<br/>
password: <input id="password" name="password" name="loginPassword" Id="loginPassword" type="password" value=""/>
<br/>
<input type="submit" id="id_login" value="Login">
</form>
</div>
<div>
<form id="signUpUser" name="SignUpForm" action="signup" method="post">
<br/>
Full Name: <input id="name" name="name" name="name" type="text" value=""/>
<br/>
Email: <input id="signupEmail" name="email" name="signupEmail" type="text" value=""/>
<br/>
password: <input id="password" name="password" name="signUpPassword" Id="signUpPassword" type="password" value=""/>
<br/>
<input type="submit" id="id_signUp" value="Sign Up">
</form>
</div>
</body>
</html>
login
行动后:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<head>
<title>Welcome to m0m0</title>
</head>
<body>
<div>
<form id="loginUser" name="loginForm" action="login" method="post">
<br/>
Email: <input id="loginEmail" name="email" name="loginEmail" type="text" value="a@b.com"/>
<br/>
password: <input id="password" name="password" name="loginPassword" Id="loginPassword" type="password" value=""/>
<br/>
<input type="submit" id="id_login" value="Login">
</form>
</div>
<div>
<form id="signUpUser" name="SignUpForm" action="signup" method="post">
<br/>
Full Name: <input id="name" name="name" name="name" type="text" value=""/>
<br/>
Email: <input id="signupEmail" name="email" name="signupEmail" type="text" value="a@b.com"/>
<br/>
password: <input id="password" name="password" name="signUpPassword" Id="signUpPassword" type="password" value=""/>
<br/>
<input type="submit" id="id_signUp" value="Sign Up">
</form>
</div>
</body>
</html>
答案 0 :(得分:2)
spring将参数绑定到两个模型,因为它不考虑绑定的模型名称(和btw。请求中没有关于模型名称的信息)
每个表单由不同的控制器处理。
但是这个方法可以处理两个?
@RequestMapping("/login")
public String login(@ModelAttribute("loginUser") User user,
BindingResult result, @ModelAttribute("signUpUser") User signUpUser,
BindingResult signUpResult, ModelMap model, HttpServletRequest request,
HttpServletResponse response) {
// Here, both user and signUpUser have the same value (WHY?)
// But I dint fill the sign up form at all
loginFormValidator.validate(user, result);
if(Errors in `result`)
return "forward:/home";
// Authentication Logic
request.getSession().setAttribute("s_user_obj", some_variable);
return "forward:/home";
}
编辑:
public class Form {
private User loginUser;
private User signupUser;
.....
}
然后你可以做以下
<form:input path="loginUser.name" />
和
<form:input path="signupUser.name" />
在这种情况下,你只有1个模型属性,而不是2个