[英]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.