方法一:@initbind注解的形式添加绑定对象(官方推荐用法)
@InitBinder
public voidinitCategoryBean(WebDataBinder binder){
binder.setFieldDefaultPrefix("category.");
}
/**
* 修改分类
*/
@RequestMapping("/alterCategory")
publicString modifyCategory(Category category,Model model) {
try{
categoryService.alterCategory(category);
model.addAttribute("message","修改分类成功");
}catch(Exception e) {
model.addAttribute("message","修改分类失败");
}
return"/jsp/message.jsp";
}
JSP页面:
<input type="hidden" name="category.id" value=" ${id }"/>
<table width="500px;">
<tr>
<td>分类名称:</td>
<td><inputtype="text" name="category.name" style="width:200px"></td>
</tr>
方法二:添加@ModelAttribute("category")注解(有时好像没用..后来再看)
@RequestMapping("/alterCategory")
publicString modifyCategory(@ModelAttribute("category") Categorycategory,Model model) {
try{
categoryService.alterCategory(category);
model.addAttribute("message","修改分类成功");
}catch(Exception e) {
model.addAttribute("message","修改分类失败");
}
return"/jsp/message.jsp";