概念:提交后,如果页面出现错误, 将刚才提交的数据回显到刚才的提交页面。
POJO数据回显方法
pojo数据传入controller方法后,springmvc自动将pojo数据放到request域,key等于pojo类型(首字母小写)
如果页面显示数据类型与controller方法传参类型不一致,则数据回显失败,需要用下面方法解决。
@modelAttribute
//@ModelAttribute可以指定pojo回显到页面在request中的key
@RequestMapping("/queryItems")
public String queryitems(Model model, @ModelAttribute("items") @Validated(value =
{ValidGroup1.class})ItemsCustom itemsCustom, BindingResult bindingResult)t
它还有其他的功能
@modelAttribute还可以将方法的返回值传到页面(不用使用requestMapping注解,直接在页面可以用${itemTypes.属性}调用)
//商品分类
//itemtypes表示最终将方法返回值放在request中的key
@ModelAttribute("itemTypes")
public Map<String,String> getItemTypes(){
Map<String,String> itemTypes = new HashMap<String,String>();
itemTypes.put("101","数码");
itemTypes.put("102","母婴");
return itemTypes;
}
还可以用model显式将pojo提交到页面
model.addAttribute("items",itemsCustom);
对于简单类型的数据回显只能通过model 提交实现
model.addAttribute("id",id);