2012-09-18 2 views
0

이것은 아마도 매우 간단한 오류 일 것입니다. 도와주세요. 나는 두 <form:form> 태그와 home.jsp 페이지가 각 형태가 다른 컨트롤러에 의해 처리됩니다Java - 동일한보기에서 두 개의 다른 @ModelAttribute가 동일한 값을 사용합니다.

<%@ 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> 

합니다.

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

} 

: 내가 채울 때 문제가있다 (단지) 양식 중 하나를 제출 버튼을 클릭, 모두 ModelAttribute의이 같은 값

로 채워지고 내 컨트롤러에 특별한 아무것도 업데이트 :

생성 된 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=""/> 

     <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 AFTER : 행동 login 전에.이 BTW 바인딩 (및 모델명을 고려하지 않기 때문에

<!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="[email protected]"/> 

     <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="[email protected]"/> 

     <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

이것은 버그처럼 보이지만 더 자세히보고 싶습니다. 이 JSP에서 생성 된 HTML을 게시 할 수 있습니까? –

+0

@subirkumarsao 질문을 HTML로 업데이트했습니다. 사전에 감사합니다 – th3an0maly

답변

2

봄, 두 모델의 매개 변수를 결합 어떠한 정보에 대한가 없습니다 요청의 모델 이름)

각 양식은 다른 컨트롤러에 의해 처리됩니다.

그러나이 방법은 둘 다 처리합니까?

@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" /> 

이 경우에는 2가 아닌 1 개의 모델 속성 만있을 것입니다.

+0

"바인딩에 대한 모델 이름을 고려하지 않았습니다"및 "..에 대한 정보가 없습니다"라는 의미를 이해하지 못합니다.'ModelMap model'을'login() '의 인수로 제공했습니다 두 번째 질문 : 아니요, 서명 양식을 처리하는 다른 방법으로 'SignUp()'이 있습니다. 초기에, 각 메소드는 하나의 (관련)'@ ModelAttribute'만을가집니다. 나는'signUpUser'와'user'가 그들의 필드에 같은 값을 가지고 있음을 증명하기 위해'signUpUser'를 포함하기 위해'login()'을 수정했습니다. – th3an0maly

+2

스프링은 singup form과 login form을 구분할 수 없습니다. 왜냐하면 둘 다 user 유형이기 때문입니다. 양식을 제출할 때 모델 속성 이름은 요청 본문의 일부가 아닙니다. 로그인 모델 또는 가입 모델로 취급할지 여부는 귀하에게 달려 있습니다. 봄이 처리하지 않습니다. –

+0

거기에는'path = "loginUser.name"'과 같은 것을 할 수있는 방법이있어서 Spring은 어떤 @ ModelAttribute를 다루는 지 알고 있습니다. 이 게시물의 두 번째 답변을 참조하십시오. http://stackoverflow.com/questions/12230216/how-to-update-two-entities-model-elements-from-one-jsp-in-spring-mvc – th3an0maly

관련 문제