数据回显即 表单提交失败后,原来提交的数据重新在提交页面上显示,不需要全部重新填写。在页面上数据回显本质上就是获取reqeust域的值
一、pojo数据默认回显方法
springmvc默认对pojo数据进行回显。pojo数据传入controller方法后,是springmvc自动将pojo数据放到request域,key等与pojo类型(首字母小写)
controller中传入User :
@RequestMapping(value = "save", method = RequestMethod.POST)
public String save(User user) {
、、、
}
jsp中用user 作为key:
<input type="text" name="username" value="${user.username}" class="form-control">
二、使用@ModelAttribute注解
controller:
@RequestMapping(value = "save", method = RequestMethod.POST)
public String save(@ModelAttribute("editUser") User user) {
、、、
}
jsp:
<input type="text" name="username" value="${editUser.username}" class="form-control">
注:被@ModelAttribute注释的方法会在此controller 每个方法执行前被执行,所以可用于回显公共数据
controller:
@ModelAttribute("typelist")
public Map<String,String> getTypeList(){
HashMap<String,String> typelist= new HashMap<String,String>();
typelist.put("0","男装");
typelist.put("1","女装");
return typelist;
}
jsp:
<select name="typelist">
<c:foreach items="${typelist}" var="type">
<option value="${type.key}">${type.value}</option>
</c:foreach>
</select>