zoukankan      html  css  js  c++  java
  • SpringMVC学习--数据回显

    • 简介

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

    • 简单数据类型

      对于简单数据类型,如:Integer、StringFloat等使用Model将传入的参数再放到request域实现显示。

    1 @RequestMapping(value="/editItems",method={RequestMethod.GET})
    2     public String editItems(Model model,Integer id)throws Exception{
    3         //传入的id重新放到request域
    4         model.addAttribute("id", id);
    5 }
    • POJO类型

      springmvc默认支持pojo数据回显,springmvc自动将形参中的pojo重新放回request域中,requestkeypojo的类名(首字母小写),如下:

    1 @RequestMapping("/editItemSubmit")
    2     public String editItemSubmit(Integer id,ItemsCustom itemsCustom)throws Exception{
    3 }

      springmvc自动将itemsCustom放回request,相当于调用下边的代码:model.addAttribute("itemsCustom", itemsCustom);

      页面中的从“itemsCustom”中取数据。

      如果key不是pojo的类名(首字母小写),可以使用@ModelAttribute完成数据回显。

      @ModelAttribute作用如下:

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

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

    1 // 商品修改提交
    2     @RequestMapping("/editItemSubmit")
    3     public String editItemSubmit(Model model,@ModelAttribute("item") ItemsCustom itemsCustom){
    4 }

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

      2、将方法返回值暴露为模型数据传到视图页面

     1 //商品分类
     2     @ModelAttribute("itemtypes")
     3     public Map<String, String> getItemTypes(){
     4         
     5         Map<String, String> itemTypes = new HashMap<String,String>();
     6         itemTypes.put("101", "数码");
     7         itemTypes.put("102", "母婴");
     8         
     9         return itemTypes;
    10     }

      商品类型:

    1 <select name="itemtype">
    2     <c:forEach items="${itemtypes }" var="itemtype">
    3         <option value="${itemtype.key }">${itemtype.value }</option>        
    4     </c:forEach>
    5 </select>
  • 相关阅读:
    ORACLE 循环
    C#生成指定数目的互不相同的随机数(转)
    Oracle自治事务的介绍
    螺旋矩阵--我的实现方法
    一个体育生的编程之路(一)
    判断十二星座——我的算法,大家看是不是比较简便
    简单的排序算法——插入排序,选择排序,交换排序(冒泡排序,快速排序)
    使用VC和DirectShow从摄像头中读取图像(一)
    1999那个追着打我的女生
    我自己写的几个C++常用方法
  • 原文地址:https://www.cnblogs.com/lcngu/p/5517877.html
Copyright © 2011-2022 走看看