简体   繁体   English

Spring 3中的默认对象mvc SessionAttributes当会话到期时

[英]Default objects in spring 3 mvc SessionAttributes when session expired

I think im confused a bit about session annotation in spring mvc. 我想我对spring mvc中的会话注释有点困惑。

I have code like this (2 steps form sample, step 1 user data, step 2 address) 我有这样的代码(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";
        }

// .........
}

Now if i submit page after my session expires i get error 现在如果我在会话结束后提交页面,我会收到错误

org.springframework.web.HttpSessionRequiredException: Session attribute 'user' required - not found in session org.springframework.web.HttpSessionRequiredException:会话属性'user'required - 在session中找不到

How do i handle it? 我该如何处理? i would like to have 2 options 我想有2个选择

  1. i create empty objects if missing in session and accept submit 我在会话中缺少并创建空对象并接受提交
  2. i forward back to user form with some message 我带回一些消息转发回用户表单

Im still in early stage of learning Spring so sorry if its something very obvious, i just cant see it. 我仍然处于学习Spring的早期阶段,如果它非常明显,我很难过,我只是无法看到它。

ps. PS。 is that even the good way to solve this kind of form in spring mvc or would you recomment different approach? 即使是在春季mvc解决这种形式的好方法,还是你会推荐不同的方法?

1.i create empty objects if missing in session and accept submit 1.i如果在会话中缺少则创建空对象并接受提交

Use @ModelAttribute("user") -annotated method to provide the default value 使用@ModelAttribute("user") annotated方法提供默认值

2.i forward back to user form with some message 2.i使用一些消息转发回用户表单

Use @ExceptionHandler(HttpSessionRequiredException.class) -annotated method 使用@ExceptionHandler(HttpSessionRequiredException.class)方法

Try to check here: 试着在这里查看:

http://forum.springsource.org/showthread.php?t=63001&highlight=HttpSessionRequiredException 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";
    }
}

According to the Spring 3.0 reference manual , it looks like @SessionAttributes is meant to be used on a type that you want to be stored transparently in the session, such as a "Command" or a form-backing object. 根据Spring 3.0参考手册 ,看起来@SessionAttributes用于要在会话中透明存储的类型,例如“Command”或表单支持对象。 I don't think you would want to store a Controller in session. 我认为您不希望将Controller存储在会话中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM