我以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