SpringMVC的数据回现

一、什么是数据回显

数据提交后,如果出现错误,将刚才提交的数据回显到刚才的提交页面。

二、pojo数据回显方法

1、springmvc默认对pojo数据进行回显。
pojo数据传入controller方法后,springmvc自动将pojo数据放到request域,key等于pojo类型(首字母小写)

使用@ModelAttribute指定pojo回显到页面在request中的key

2、@ModelAttribute还可以将方法的返回值传到页面

在商品查询列表页面,通过商品类型查询商品信息。
在controller中定义商品类型查询方法,最终将商品类型传到页面。

@ModelAttribute("itemTypes")
public Map<String,String> getItemsType(){
Map<String,String> map = new HashMap<>();
map.put("1","数码产品");
map.put("2","生活用品");
return map;
}

页面上可以得到itemTypes数据。

商品类型:
测试如下:

三、最简单的pojo数据回现

使用处理器适配器的默认支持的Model对象,将其作为controller的形参来回现数据。

@RequestMapping("/updateitems")
public String updateitems(Model model,Integer id, @Validated(value = {VaildatorGroup1.class}) ItemsCustom itemsExtend,
BindingResult bindingResult) throws Exception{

if(bindingResult.hasErrors()){
List errors = bindingResult.getAllErrors();
for (ObjectError error: errors) {
System.out.println(error.getDefaultMessage());
}
model.addAttribute("errors",errors);
model.addAttribute("itemsExtend",itemsExtend);
return "updateitem";
}
itemsService.updateitems(id,itemsExtend);
return"redirect:queryItems.action";
}

四、简单类型的回现

用最简单方法使用model。

model.addAttribute("id", id);

posted @ 2017-08-14 14:19  一条路上的咸鱼  阅读(337)  评论(0)    收藏  举报