[英]Default objects in spring 3 mvc SessionAttributes when session expired
我想我對spring mvc中的會話注釋有點困惑。
我有這樣的代碼(2個步驟構成示例,第1步用戶數據,第2步地址)
@SessionAttributes({"user", "address"})
public class UserFormController {
@RequestMapping(method = RequestMethod.GET)
public ModelAndView show( ModelAndView mv ){
mv.addObject( new User() );
mv.addObject( new Address() );
mv.setViewName("user_add_page");
return mv;
}
@RequestMapping(method = RequestMethod.POST)
public String processForm( User user, BindingResult result ){
new UserValidator().validate(user, result);
if( result.hasErrors() ){
return "user_add_page";
}else{
return "redirect:/user_form/user_add_address";
}
// .........
}
現在如果我在會話結束后提交頁面,我會收到錯誤
org.springframework.web.HttpSessionRequiredException:會話屬性'user'required - 在session中找不到
我該如何處理? 我想有2個選擇
我仍然處於學習Spring的早期階段,如果它非常明顯,我很難過,我只是無法看到它。
PS。 即使是在春季mvc解決這種形式的好方法,還是你會推薦不同的方法?
1.i如果在會話中缺少則創建空對象並接受提交
使用@ModelAttribute("user")
annotated方法提供默認值
2.i使用一些消息轉發回用戶表單
使用@ExceptionHandler(HttpSessionRequiredException.class)
方法
試着在這里查看:
http://forum.springsource.org/showthread.php?t=63001&highlight=HttpSessionRequiredException
@Controller
@RequestMapping(value="/simple_form")
@SessionAttributes("command")
public class ChangeLoginController {
@ModelAttribute("command")
public MyCommand createCommand() {
return new MyCommand();
}
@RequestMapping(method = RequestMethod.GET)
public String get() {
return "form_view";
}
@RequestMapping(method = RequestMethod.POST)
public String post(@ModelAttribute("command") MyCommand command) {
doSomething(command); // execute business logic
return "form_view";
}
}
根據Spring 3.0參考手冊 ,看起來@SessionAttributes用於要在會話中透明存儲的類型,例如“Command”或表單支持對象。 我認為您不希望將Controller存儲在會話中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.