Jér*_*nge 5 java data-binding spring spring-mvc modelattribute
以下是一个简单的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";
}
}
Run Code Online (Sandbox Code Playgroud)
我在某处读到: (...) 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会保留单独的模型副本吗?
的setupItem将要被每个请求调用一次到任何的@RequestMapping在该控制器的方法,右之前@RequestMapping被调用的方法.所以对于你的addItem方法,流程将是 - 调用setupItem,创建一个名为的模型属性item,因为你的addItem参数也被标记@ModelAttribute,此时item将使用POST参数增强.
| 归档时间: |
|
| 查看次数: |
2376 次 |
| 最近记录: |