springMVC学习(8)-数据回显
什么是数据回显:
提交后,如果出现错误(或者别的情况),将刚才提交的数据回显到刚才的提交页面。
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>