Spring MVC 数据回显

springmvc 的数据回显

原创 2015年08月02日 11:39:00
 

1        数据回显

1.1    什么数据回显

提交后,如果出现错误,将刚才提交的数据回显到刚才的提交页面。

即表单提交失败不需要再回到表单页面重新填写,原来提交的数据需要重新在页面上显示。

1.2    pojo数据回显方法

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

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

  修改信息editItems()方法中的标识是   model.addAttribute("itemsCustom",itemsCustom);

       editItems.jsp页面接收的标识为  <input type="hidden"name="id" value="${itemsCustom.id}"/>

       提交修改的方法    public String editItemsSubmit(Modelmodel,HttpServletRequest request,Integer id,@Validated(value={ValidGrouop1.class})  ItemsCustom itemsCustom,BindingResult bindingResult)throws Exception

 

        三者一致方可自动回显

使用@ModelAttribute指定pojo回显到页面在request中的key

1、绑定请求参数到pojo并且暴露为模型数据传到视图页面

此方法可实现数据回显效果。

@ModelAttribute("item") 中的item 对应ItemsCustom itemsCustom 的itemsCustom 为itemsCustom的别名,用于保持和页面的"${item.name }" 中的item 一致实现数据回显

// 商品修改提交

   @RequestMapping("/editItemSubmit")

   public String editItemSubmit(Model model , @ModelAttribute("item") ItemsCustom itemsCustom)

  

页面:

<tr>

   <td>商品名称</td>

   <td><input type="text"name="name" value="${item.name}"/></td>

</tr>

<tr>

   <td>商品价格</td>

   <td><input type="text"name="price" value="${item.price}"/></td>

</tr>

如果不用@ModelAttribute也可以使用model.addAttribute("item", itemsCustom)完成数据回显。

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

在商品查询列表页面,通过商品类型查询商品信息。

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

   //商品分类

   //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;

      }

页面上可以得到itemTypes数据。

商品分类:

<select name="itemtype">

   <c:forEach items="${itemtypes }" var="itemtype">

      <option value="${itemtype.key}">${itemtype.value }</option>   

   </c:forEach>

</select>

3、使用最简单方法使用model,可以不用@ModelAttribute

@RequestMapping("/editItemsSubmit")

      public StringeditItemsSubmit(Model model

           ,HttpServletRequest request,

           Integerid,

           @ModelAttribute("items")@Validated(value={ValidGrouop1.class})  ItemsCustom itemsCustom,

           BindingResultbindingResult)throwsException{

        //获取验证错误信息

        if(bindingResult.hasErrors())

        {

           //输出错误信息

           List<ObjectError>allerrors=bindingResult.getAllErrors();

           for(ObjectError error:allerrors)

           {

              System.out.println(error.getDefaultMessage());

           }

           //错误信息传递到页面

           model.addAttribute("allErrors",allerrors);

           //使用model 的方式使数据回显

           model.addAttribute("items",itemsCustom);

           return "items/editItems";

        }

        //调用service更新商品信息,页面需要将商品信息传到此方法

        itemsService.updateItems(id,itemsCustom);

        //重定向  不用加跟路径

        //return "redirect:queryItems.action";

        //页面转发

        return "forward:queryItems.action";

      }

1.3    简单类型数据回显

使用最简单方法使用model。

model.addAttribute("id", id);

仅供自己查阅参考使用,如有冒犯请留言联系

转载自: https://blog.csdn.net/u012373815/article/details/47205657
原文地址:https://www.cnblogs.com/webyyq/p/8676269.html