什么是数据回显:
提交后,如果出现错误(或者别的情况),将刚才提交的数据回显到刚才的提交页面。
pojo数据回显方法:
一、springmvc默认对pojo数据进行回显。
比如现在的jsp页面提示出现错误,页面自动显示之前的数据:
因为pojo数据传入controller方法后,springmvc自动将pojo数据放到request域,key等于pojo类型(首字母小写)
这里默认将数据放到itemsCustom中
1 //商品信息修改提交 2 @RequestMapping("/editItemsSubmit") 3 public String editItemsSubmit(Model model, 4 HttpServletRequest request, 5 Integer id, 6 @Validated(value={ValidGroup1.class}) ItemsCustom itemsCustom,BindingResult bindingResult) 7 throws Exception { 8 9 if(bindingResult.hasErrors()){ 10 List<ObjectError> allErrors = bindingResult.getAllErrors(); 11 for(ObjectError objectError : allErrors){ 12 System.out.println(objectError.getDefaultMessage()); 13 } 14 15 // 将错误信息传到页面 16 model.addAttribute("allErrors", allErrors); 17 18 return "items/editItems"; 19 } 20 21 itemsService.updateItems(id, itemsCustom); 22 return "success"; 23 }
jsp:
1 <form id="itemForm" action="${pageContext.request.contextPath }/items/editItemsSubmit.action" method="post" > 2 <input type="hidden" name="id" value="${itemsCustom.id }"/> 3 修改商品信息: 4 <table width="100%" border=1> 5 <tr> 6 <td>商品名称</td> 7 <td><input type="text" name="name" value="${itemsCustom.name }"/></td> 8 </tr> 9 <tr> 10 <td>商品价格</td> 11 <td><input type="text" name="price" value="${itemsCustom.price }"/></td> 12 </tr> 13 <tr> 14 <td>商品生产日期</td> 15 <td><input type="text" name="createtime" value="<fmt:formatDate value="${itemsCustom.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td> 16 </tr> 17 <%-- <tr> 18 <td>商品图片</td> 19 <td> 20 <c:if test="${item.pic !=null}"> 21 <img src="/pic/${item.pic}" width=100 height=100/> 22 <br/> 23 </c:if> 24 <input type="file" name="pictureFile"/> 25 </td> 26 </tr> --%> 27 <tr> 28 <td>商品简介</td> 29 <td> 30 <textarea rows="3" cols="30" name="detail">${itemsCustom.detail }</textarea> 31 </td> 32 </tr> 33 <tr> 34 <td colspan="2" align="center"><input type="submit" value="提交"/> 35 </td> 36 </tr> 37 </table> 38 </form>
二、使用@ModelAttribute指定pojo回显到页面在request中的key:
jsp页面修改为:
1 <form id="itemForm" action="${pageContext.request.contextPath }/items/editItemsSubmit.action" method="post" > 2 <input type="hidden" name="id" value="${items.id }"/> 3 修改商品信息: 4 <table width="100%" border=1> 5 <tr> 6 <td>商品名称</td> 7 <td><input type="text" name="name" value="${items.name }"/></td> 8 </tr> 9 <tr> 10 <td>商品价格</td> 11 <td><input type="text" name="price" value="${items.price }"/></td> 12 </tr> 13 <tr> 14 <td>商品生产日期</td> 15 <td><input type="text" name="createtime" value="<fmt:formatDate value="${items.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td> 16 </tr> 17 <%-- <tr> 18 <td>商品图片</td> 19 <td> 20 <c:if test="${item.pic !=null}"> 21 <img src="/pic/${item.pic}" width=100 height=100/> 22 <br/> 23 </c:if> 24 <input type="file" name="pictureFile"/> 25 </td> 26 </tr> --%> 27 <tr> 28 <td>商品简介</td> 29 <td> 30 <textarea rows="3" cols="30" name="detail">${items.detail }</textarea> 31 </td> 32 </tr> 33 <tr> 34 <td colspan="2" align="center"><input type="submit" value="提交"/> 35 </td> 36 </tr> 37 </table> 38 </form>
controller添加@ModelAttribute("items")指定转到页面中的key为items:
1 //商品信息修改提交 2 @RequestMapping("/editItemsSubmit") 3 public String editItemsSubmit(Model model, 4 HttpServletRequest request, 5 Integer id, 6 @ModelAttribute("items") @Validated(value={ValidGroup1.class}) ItemsCustom itemsCustom,BindingResult bindingResult) 7 throws Exception { 8 9 if(bindingResult.hasErrors()){ 10 List<ObjectError> allErrors = bindingResult.getAllErrors(); 11 for(ObjectError objectError : allErrors){ 12 System.out.println(objectError.getDefaultMessage()); 13 } 14 15 // 将错误信息传到页面 16 model.addAttribute("allErrors", allErrors); 17 18 return "items/editItems"; 19 } 20 21 itemsService.updateItems(id, itemsCustom); 22 return "success"; 23 }
页面提交后,报错了,依然能够回显:
三、@ModelAttribute还可以将方法的返回值传到页面
在商品查询列表页面,通过商品类型查询商品信息。
在controller中定义商品类型查询方法,最终将商品类型传到页面。
@Controller @RequestMapping("/items") public class ItemsController { @Autowired private ItemsService itemsService; // 商品分类 //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; } .... }
itemList.jsp查看:
1 <td> 2 商品名称:<input name="itemsCustom.name" /> 3 商品类型: 4 <select name="itemtype"> 5 <c:forEach items="${itemtypes }" var="itemtype"> 6 <option value="${itemtype.key }">${itemtype.value }</option> 7 </c:forEach> 8 </select> 9 </td>
访问:http://localhost:8080/springMVC/items/findItems.action
四、不用@ModelAttribute,使用最简单的Model来回显:
Controller:
1 //商品信息修改提交 2 @RequestMapping("/editItemsSubmit") 3 public String editItemsSubmit(Model model, 4 HttpServletRequest request, 5 Integer id, 6 @Validated(value={ValidGroup1.class}) ItemsCustom itemsCustom,BindingResult bindingResult) 7 throws Exception { 8 9 if(bindingResult.hasErrors()){ 10 List<ObjectError> allErrors = bindingResult.getAllErrors(); 11 for(ObjectError objectError : allErrors){ 12 System.out.println(objectError.getDefaultMessage()); 13 } 14 15 // 将错误信息传到页面 16 model.addAttribute("allErrors", allErrors); 17 18 //可以直接使用model将提交pojo回显到页面 19 model.addAttribute("items", itemsCustom); 20 21 return "items/editItems"; 22 } 23 24 itemsService.updateItems(id, itemsCustom); 25 return "success"; 26 }
前台jsp页面不变;
依然可以回显;
五、简单数据类型回显:
使用最简单方法使用model。
model.addAttribute("id", id);
原文:http://www.cnblogs.com/tenWood/p/6329586.html