【发布时间】:2012-07-14 16:32:07
【问题描述】:
以下是一个简单的 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 会保留单独的模型副本吗?
【问题讨论】:
标签: java spring data-binding spring-mvc modelattribute