什么是数据回显
表单提交后,如果出现错误,将刚才提交的数据回显到刚才提交的页面
1.springmvc默认对pojo数据进行回显
pojo数据传入Controller方法后,springmvc自动将pojo数据放到request域,key等于pojo类型(首字母小写)
2.使用@ModelAttribute指定pojo回显到页面在request中的key
public String editUserSumit(Model model, Integer id,@ModelAttribute("user") @Validated(value=(ValidationGroup1.class)) UserCustom userCustom, BindingResult bindingResult) throws Exception {}
@ModelAttribute还可以将方法的返回值传到页面
jsp页面:
<select name="allName">
<c:forEach items="${names }" var="item">
<option value="${item.key }">${item.value }</option>
</c:forEach>
</select>
Controller:
@ModelAttribute("names")
public Map<String, String> userNames(){
Map<String, String> map = new HashMap<>();
map.put("1", "麦兜");
map.put("2", "奥特曼");
return map;
}
3.使用最简单的方法 使用 model,可以不用@ModelAttribute
model.addAttribute("user", userCustom);
4.简单类型的数据回显使用 model,不用@ModelAttribute
model.addAttribute("id", id);
转载请注明原文地址: https://ju.6miu.com/read-658944.html