Spring Framework 3 和会话属性 [英] Spring Framework 3 and session attributes

查看:28
本文介绍了Spring Framework 3 和会话属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Spring 控制器的 GET 请求处理程序中设置了要请求的表单对象.用户第一次进入页面时,应创建一个新的表单对象并将其设置为请求.如果用户发送表单,则根据请求填充表单对象,现在表单对象具有所有用户给定的属性.然后验证表单,如果验证正常,则将表单保存到数据库中.如果未验证表单,我想将表单对象保存到会话,然后重定向到 GET 请求处理页面.当请求被重定向到 GET 处理程序时,它应该检查会话是否包含表单对象.

I have form object that I set to request in GET request handler in my Spring controller. First time user enters to page, a new form object should be made and set to request. If user sends form, then form object is populated from request and now form object has all user givern attributes. Then form is validated and if validation is ok, then form is saved to database. If form is not validated, I want to save form object to session and then redirect to GET request handling page. When request is redirected to GET handler, then it should check if session contains form object.

我发现 Spring 中有 @SessionAttributes("form") 注释,但由于某种原因,以下不起作用,因为第一次,会话属性表单为空并且它给出了错误:

I have figured out that there is @SessionAttributes("form") annotation in Spring, but for some reason following doesnt work, because at first time, session attribute form is null and it gives error:

org.springframework.web.HttpSessionRequiredException: Session attribute 'form' required - not found in session

这是我的控制器:

@RequestMapping(value="form", method=RequestMethod.GET)
public ModelAndView viewForm(@ModelAttribute("form") Form form) {
    ModelAndView mav = new ModelAndView("form");      
    if(form == null) form = new Form();
    mav.addObject("form", form);
    return mav;
}

@RequestMapping(value="form", method=RequestMethod.POST)
@Transactional(readOnly = true)
public ModelAndView saveForm(@ModelAttribute("form") Form form) {
    FormUtils.populate(form, request);
    if(form.validate())
    {
        formDao.save();         
    }
    else 
    {
        return viewForm(form);
    }
    return null;
}

推荐答案

@SessionAttribute 的工作是将现有模型对象绑定到会话.如果它还不存在,您需要定义它.在我看来,这是不必要的混乱,但请尝试这样的事情:

The job of @SessionAttribute is to bind an existing model object to the session. If it doesn't yet exist, you need to define it. It's unnecessarily confusing, in my opinion, but try something like this:

@SessionAttributes({"form"})
@Controller
public class MyController {

   @RequestMapping(value="form", method=RequestMethod.GET)
   public ModelAndView viewForm(@ModelAttribute("form") Form form) {
       ModelAndView mav = new ModelAndView("form");      
       if(form == null) form = new Form();
       mav.addObject("form", form);
       return mav;
   }

   @RequestMapping(value="form", method=RequestMethod.POST)
   @Transactional(readOnly = true)
   public ModelAndView saveForm(@ModelAttribute("form") Form form) {
      // ..etc etc
   }
}

请注意,@SessionAttributes 是在类上声明的,而不是在方法上声明的.你可以放任何你喜欢的地方,真的,但我认为这在课堂上更有意义.

Note that the @SessionAttributes is declared on the class, rather than the method. You can put wherever you like, really, but I think it makes more sense on the class.

在我看来,有关此内容的文档 可能更清楚.

这篇关于Spring Framework 3 和会话属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆