Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-同一视图上的两个不同@ModelAttribute占用相同的值_Java_Spring_Jsp_Modelattribute - Fatal编程技术网

Java-同一视图上的两个不同@ModelAttribute占用相同的值

Java-同一视图上的两个不同@ModelAttribute占用相同的值,java,spring,jsp,modelattribute,Java,Spring,Jsp,Modelattribute,这可能是某个地方的一个非常简单的错误。请帮忙。 我有一个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/l

这可能是某个地方的一个非常简单的错误。请帮忙。 我有一个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>
更新:

下面是生成的HTMLs。 在
登录之前
操作:

<!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>
<!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>

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";
}

编辑:

然后您可以执行以下操作

<form:input path="loginUser.name" />



在本例中,您将只有1个模型属性,而不是2个

它看起来像一个bug,但我想进一步研究。您可以发布这个JSP生成的HTML吗。@subirkumarsao我已经用HTML更新了这个问题。谢谢你,我不明白你的意思是“它没有考虑绑定的模型名”和“没有关于……的信息”。我已经提供了<代码>模型映射模型< /代码>作为一个参数,以“代码> Login())/代码>到你的第二个问题:不,还有另一个方法,叫做“代码>符号())/代码>来处理注册表单。最初,每个方法只有一个(相关的)
@modeldattribute
。我修改了
login()
以包含
signUpUser
,以证明
signUpUser
user
在其字段中具有相同的值。spring无法区分singup表单和login表单,因为两者都属于user类型。提交表单时,模型属性名称不是请求正文的一部分。因此,将其视为登录模型还是注册模型取决于您。spring不会处理这个问题。有没有一种方法可以像
path=“logiuser.name”
那样让spring知道它处理的是哪个
@ModelAttribute
?请参考本文中的第二个答案:创建2个表单:表单元素(就像在您的文章中一样),在第一个答案中,您可以使用路径前缀“loginUser”。在第二个答案中,您可以使用“signupUser”。让我们
public class Form {
    private User loginUser;
    private User signupUser;
    .....
}
<form:input path="loginUser.name" />
<form:input path="signupUser.name" />