The following is a simple Spring form controller to handle 'add item' user requests:
@Controller
@RequestMapping("/addItem.htm")
public class AddItemFormController {
@Autowired
ItemService itemService;
@RequestMapping(method = RequestMethod.GET)
public String setupForm(ModelMap model) {
return "addItem";
}
@ModelAttribute("item")
public Item setupItem() {
Item item = new Item();
return item;
}
@RequestMapping(method = RequestMethod.POST)
protected String addItem(@ModelAttribute("item") Item item) {
itemService.addItem(item);
return "itemAdded";
}
}
I read somewhere that: (...) the @ModelAttribute is also pulling double duty by populating the model with a new instance of Item before the form is displayed and then pulling the Item from the model so that it can be given to addItem() for processing.
My question is, when and how often is setupItem()
going to be called precisely? Will Spring keep a separate model copy if user requests multiple add item?