springmvc数据回显问题

来源:互联网 发布:西南财大网络教育官网 编辑:程序博客网 时间:2024/05/08 01:06

数据回显就是在页面提交请求后,如果出现错误,则将刚才提交的数据回显到刚才的提交页面

1、springmvc默认对pojo数据进行回显

Pojo数据传入controller方法后,springmvc自动将pojo数据放到request域,key等于pojo类型(首字母小写)。如:

<tr><td>商品价格</td><td><input type="text" name="price" value="$<span style="background-color: rgb(255, 255, 102);">{itemsCustom</span>.price }"/></td></tr>
页面中的取值key为itemsCustom,要和 controller方法形参中的参数名称一致,这样springmvc就会自动的进行数据的回显

@RequestMapping("/editItemsSubmit")public String editItemsSubmit(Model model,HttpServletRequest request,Integer id,String name,@Validated(value={ValidGroup1.class}) ItemsCustom <span style="background-color: rgb(255, 255, 102);">itemsCusto</span>m,BindingResult bindingResult) throws Exception{}

2、使用@ModelAttribute("items"):可以指定pojo回显到页面在request域中的key

//@ModelAttribute("items"):可以指定pojo回显到页面在request域中的key@RequestMapping("/editItemsSubmit")public String editItemsSubmit(Model model,HttpServletRequest request,Integer id,String name,<span style="background-color: rgb(255, 255, 102);">@ModelAttribute("items")</span> @Validated(value={ValidGroup1.class}) ItemsCustom itemsCustom,BindingResult bindingResult) throws Exception{}
页面中获取参数的key就是@ModelAttribute中定义的key

<table width="100%;" border="1"><tr><td>商品名称</td><td><input type="text" name="name" value="${<span style="background-color: rgb(255, 255, 102);">items</span>.name }"/></td></tr><tr><td>商品价格</td><td><input type="text" name="price" value="${<span style="background-color: rgb(255, 255, 102);">items</span>.price }"/></td></tr><tr><td>商品生产日期</td><td><input type="text" name="createtime" value="<fmt:formatDate value="${<span style="background-color: rgb(255, 255, 102);">items</span>.createtime }" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td></tr></table>

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

例如:在controller中定义商品类型查询方法,最终将商品类型传到页面。

//商品分类//itemtypes表示最终将方法返回值放在request中的key@ModelAttribute("<span style="background-color: rgb(255, 255, 102);">itemtypes</span>")public Map<String, Object> getItemTypes(){Map<String, Object> itemTypes = new HashMap<String, Object>();itemTypes.put("101", "数码");itemTypes.put("102", "母婴");return itemTypes;}
在页面上可以得到itemsTypes数据

<td>商品类型:<select name="itemtype"><c:forEach items="${<span style="background-color: rgb(255, 255, 102);">itemtypes</span> }" var="itemtype"><option value="${itemtype.key }">${itemtype.value}</option></c:forEach></select></td>
3、最简单的数据回显的方式:使用model

@RequestMapping("/editItemsSubmit")public String editItemsSubmit(Model model,HttpServletRequest request,Integer id,String name,@ModelAttribute("items") @Validated(value={ValidGroup1.class}) ItemsCustom itemsCustom,BindingResult bindingResult) throws Exception{//获取校验错误信息if(bindingResult.hasErrors()){//如果有错误//输出错误信息List<ObjectError> allErrors = bindingResult.getAllErrors();for (ObjectError objectError : allErrors) {//输出错误信息System.out.println(objectError.getDefaultMessage());}//将错误信息传到页面model.addAttribute("allErrors", allErrors);<span style="background-color: rgb(255, 255, 102);">//可以直接使用model将提交的pojo回显到页面model.addAttribute("itemsCustom",itemsCustom);</span>//出错后重新回到商品的修改页面return "items/editItems";}//调用service更新商品信息,页面需要将商品新传到此方法itemsService.updateItems(id, itemsCustom);//重定向到商品的查询列表return "redirect:queryItems.action";}
在页面上通过 ${itemsCustom.***}接收参数
4、简单类型的数据回显

springmvc不支持简单类型的数据回显,因此直接使用model即可。

model.addAttribute("id",id);





0 0
原创粉丝点击