Spring Framework 3 and session attributes

前端 未结 5 967
你的背包
你的背包 2020-12-02 14:45

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.

5条回答
  •  孤街浪徒
    2020-12-02 15:30

    if there is no defined session object so I think it's gonna be like this:

    @SessionAttributes({"form"})
        @Controller
        public class MyController {
       @RequestMapping(value="form", method=RequestMethod.GET)
       public ModelAndView viewForm() {
           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
       }
    }
    

提交回复
热议问题