Spring Framework 3 and session attributes

前端 未结 5 962
你的背包
你的背包 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:26

    @Controller
    @SessionAttributes("goal")
    public class GoalController {
    
        @RequestMapping(value = "/addGoal", method = RequestMethod.GET)
        public String addGoal(Model model) {
            model.addAttribute("goal", new Goal(11));
            return "addGoal";
        }
    
        @RequestMapping(value = "/addGoal", method = RequestMethod.POST)
        public String addGoalMinutes(@ModelAttribute("goal") Goal goal) {
            System.out.println("goal minutes " + goal.getMinutes());
            return "addMinutes";
        }
    }
    

    On page addGoal.jsp user enters any amount and submits page. Posted amount is stored in HTTP Session because of

    @ModelAttribute("goal") Goal goal
    

    and

    @SessionAttributes("goal")

    Without @ModelAttribute("goal") amount entered by user on addGoal page would be lost

提交回复
热议问题