java 如何在控制器之间传递 ModelAttribute?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12463409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I pass ModelAttribute between Controllers?
提问by th3an0maly
I'm trying to create a Home page with 2 functionalities:
我正在尝试创建一个具有 2 个功能的主页:
- Login
- Sign Up
- 登录
- 报名
I'm mapping each request to a different controller and trying to get the result back to my home.jsp
. But I'm having trouble passing only certain specific ModelAttribute
around, between Controllers. More specifically, i cant get the changed I make to the ModelMap
and BindingResult
in one controller to be reflected in others.
我将每个请求映射到不同的控制器并尝试将结果返回到我的home.jsp
. 但是我ModelAttribute
在控制器之间只传递某些特定的东西时遇到了麻烦。更具体地说,我无法在一个控制器中对ModelMap
和所做的更改BindingResult
反映在其他控制器中。
I'm sure there's something basicallywrong with what I'm doing. Please help.
我敢肯定,我正在做的事情基本上有问题。请帮忙。
There are 2 forms in my home.jsp
. One for Login:
我的home.jsp
. 一种用于登录:
<form:form name="loginForm" modelAttribute="loginUser" action="login" method="post">
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>
and the other one for Sign Up:
另一个用于注册:
<form:form name="SignUpForm" modelAttribute="signUpUser" action="signup" method="post">
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" />
<input type="submit" id="id_signUp" value="Sign Up">
</form:form>
I have 3 controllers: HomeController.java
:
我有 3 个控制器HomeController.java
:
@RequestMapping("/home")
public String showHome(ModelMap model,
@ModelAttribute("loginUser") User loginUser,
BindingResult loginResult,
@ModelAttribute("signUpUser") User signUpUser,
BindingResult signUpResult) {
return "home";
}
AuthenticationController.java
:
AuthenticationController.java
:
@RequestMapping("/login")
public String login(@ModelAttribute("loginUser") User user,
BindingResult result, ModelMap model, HttpServletRequest request,
HttpServletResponse response) {
loginFormValidator.validate(user, result);
if(Errors in `result`)
return "forward:/home";
// Authentication Logic
request.getSession().setAttribute("s_user_obj", some_variable);
return "forward:/home";
}
and ProfileController.java
:
和ProfileController.java
:
@RequestMapping("/signup")
public String signUpUser(@ModelAttribute("signUpUser") User user,
BindingResult result, ModelMap model) {
// Do stuff related to Sign Up
// Forward to home.jsp
}
When the request is forwarded to /home
, I'm getting same values in both loginUser
and signUpUser
. Worse, there are no errors i.e. the result
variable is not reflecting the ones in the previous controllers.
当请求转发到 时/home
,我在loginUser
和中都获得了相同的值signUpUser
。更糟糕的是,没有错误,即result
变量没有反映以前控制器中的错误。
I'm sure there's a better way of doing this, this is a newbie's attempt at it. Please advice.
我相信有更好的方法可以做到这一点,这是新手的尝试。请指教。
回答by Biju Kunjummen
The issue that I see here is the way @ModelAttribute("loginUser")
and @ModelAttribute("signUpUser")
is interpreted for methods by Spring - here is some reference - http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-method-args
我看到这里的问题是这样的@ModelAttribute("loginUser")
,并@ModelAttribute("signUpUser")
解释了由Spring方法-这里是一些参考- http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc。 html#mvc-ann-modelattrib-method-args
The arguments are retrieved from the model(and instantiated if necessary), but the problem part is that they are repopulated based on the request parameters, which would end up populating both your attributes the same way. The only workaround that I can think of is to explicitly retrieve the model attributes yourself in your method:
参数是从模型中检索的(并在必要时实例化),但问题在于它们是根据请求参数重新填充的,这最终会以相同的方式填充您的两个属性。我能想到的唯一解决方法是在您的方法中自己显式检索模型属性:
@RequestMapping("/home")
public String showHome(ModelMap model) {
model.get("loginUser");...
model.get("signUpUser");...
return "home";
}
Update
更新
Based on your comments, let me recommend an alternate flow and a more explicit flow:
根据您的评论,让我推荐一个替代流程和更明确的流程:
Have a method which sets both your loginUser and signUpUser model attributes this way:
有一个以这种方式设置 loginUser 和 signUpUser 模型属性的方法:
private void populateAttributes(Model model, User loginUser, User signupUser){
if (loginUser==null) loginUser = new User();
if (singupUser==null) singupUser = new User();
model.addAttribute("loginUser", loginUser);
model.addAttribute("signupUser", signupUser);
}
Now in your login flow:
现在在您的登录流程中:
@RequestMapping("/login")
public String login(User user,
BindingResult result, Model model, HttpServletRequest request,
HttpServletResponse response) {
loginFormValidator.validate(user, result);
if(Errors in `result`)
populateAttributes(user, null);
return "home";
// Authentication Logic
request.getSession().setAttribute("s_user_obj", some_variable);
populateAttributes(model, user, null);
return "home";
}
}
Similar flow in your signup:
注册中的类似流程:
@RequestMapping("/signup")
public String signUpUser(User user,
BindingResult result, Model model) {
populateAttributes(model, null, user);
return "home"
}
and showHome:
并显示主页:
@RequestMapping("/home")
public String showHome(Model model,) {
populateAttributes(model, null, null);
return "home";
}