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.
@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