以下是一个简单的 Spring 表单控制器,用于处理“添加项目”用户请求:
@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";
}
}
我在某处读到:(...) 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.
我的问题是,何时以及多久setupItem()
准确地调用一次?如果用户请求多个添加项,Spring 会保留单独的模型副本吗?