我以Spring Portlet MVC
开头,但我相信这个问题也适用于Spring MVC
。让我告诉你这个例子页面之间的重定向 - Spring MVC
@Controller
@RequestMapping(value="VIEW")//this maps to Portlet Mode VIEW
public class LoginController {
@RenderMapping("action=showRegisterPage")
//this annotation is almost the same like @RequestMapping
public String showRegisterPage() {
return "registration";
}
@ModelAttribute("loginForm")
public LoginForm getLoginForm() {
return new LoginForm();
}
}
,然后控制器注册页面
@Controller
@RequestMapping(value="VIEW")
public class RegistrationController {
@ModelAttribute("user")
public User getUser() {
return new User();
}
}
而问题是,当我从页面调用showRegisterPage方法(动作)我重定向到registration.jsp但出现这种异常
Caused by: java.lang.IllegalStateException: Neither BindingResult nor
plain target object for bean name 'user' available as request attribute
这可以通过将ModelAttribute
用户的LoginController但代码重复固定不是很合适的方式,所以我怎么能解决ŧ他是否正确?我也尝试将showRegisterPage方法移动到RegistrationController,但该链接不工作。也许有人可以详细说明如何将页面绑定到某个控制器上?
P.S. index.jsp和registration.jsp都包含<form:form>
标签,我也省略了两个控制器的样板代码。
'用户'属性需要添加到模型,然后将自动绑定到请求范围。没有这个,就会出现错误。请看Georgy的回答。 – Victor 2013-04-11 20:17:31