@SessionAttribute : When is the model initialized?

前端 未结 2 2056
一个人的身影
一个人的身影 2021-01-03 16:16

When I want a model in Session scope in Spring 3, I use the foll. annotation in a Controller:-

    @SessionAttribute(\"myModel\");

However,

2条回答
  •  天涯浪人
    2021-01-03 16:57

    You can annotate methods with @ModelAttribute, if the attribute name is the same as specified in the the @SessionAttribute annotation then the attribute will be stored in the session. Here is a complete example :

    @Controller   
    @RequestMapping(value = "/test.htm") 
    @SessionAttributes("myModel")
    public class DeleteNewsFormController {
    
        // Add you model object to the session here
        @ModelAttribute("myModel")
        public String getResultSet() {
            return "Hello";
        }
    
        //retreive objects from the session
        @RequestMapping(method = RequestMethod.GET)
        public @ResponseBody testMethod(@ModelAttribute("resultSet") String test, Model model) {
    

提交回复
热议问题