By annotating the createPriceIncrease method with @ModelAttribute , you tell spring how to initially populate the priceIncrease model value.
@SessionAttributes tells spring to automatically save the priceIncrease object in the session after each request.
Finally, @ModelAttribute in the method parameter for the "post" and "get" methods tells spring to find a model attribute called "priceIncrease".
He will know this session attribute and find it there, if possible, otherwise he will create it using the createPriceIncrease method.
@Controller @SessionAttributes({"priceIncrease"}) @RequestMapping("/priceIncrease") public class MyController { @ModelAttribute("priceIncrease") public PriceIncrease createPriceIncrease() { PriceIncrease priceIncrease = new PriceIncrease(); priceIncrease.setPercentage(20); return priceIncrease; } @RequestMapping(method={RequestMethod.POST}) public ModelAndView post(@ModelAttribute("priceIncrease") PriceIncrease priceIncrease, HttpServletRequest request, HttpServletResponse response) { ... } @RequestMapping(method={RequestMethod.GET}) public ModelAndView get(@ModelAttribute("priceIncrease") PriceIncrease priceIncrease, HttpServletRequest request, HttpServletResponse response) { ... } }
source share