java 会话过期时 spring 3 mvc SessionAttributes 中的默认对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2094179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:24:31  来源:igfitidea点击:

Default objects in spring 3 mvc SessionAttributes when session expired

javaspringspring-mvcsession

提问by Art79

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:需要会话属性“用户”-在会话中找不到

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
  1. 如果在会话中丢失,我会创建空对象并接受提交
  2. 我用一些消息转发回用户表单

Im still in early stage of learning Spring so sorry if its something very obvious, i just cant see it.

我还处于学习 Spring 的早期阶段,很抱歉,如果它的某些东西非常明显,我只是看不到它。

ps. is that even the good way to solve this kind of form in spring mvc or would you recomment different approach?

附:这是在spring mvc中解决这种形式的好方法还是你会推荐不同的方法?

采纳答案by axtavt

1.i create empty objects if missing in session and accept submit

1.如果会话中丢失,我创建空对象并接受提交

Use @ModelAttribute("user")-annotated method to provide the default value

使用@ModelAttribute("user")-annotated 方法提供默认值

2.i forward back to user form with some message

2.我用一些消息转发回用户表单

Use @ExceptionHandler(HttpSessionRequiredException.class)-annotated method

使用带@ExceptionHandler(HttpSessionRequiredException.class)注释的方法

回答by andrew0007

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";
    }
}

回答by matt b

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. I don't think you would want to store a Controller in session.

根据Spring 3.0 参考手册,看起来 @SessionAttributes 旨在用于您希望在会话中透明存储的类型,例如“命令”或表单支持对象。我认为您不想在会话中存储控制器。