• springMVC学习(6)-包装pojo类型、数组、list、Map类型参数绑定


    一、包装类型pojo参数绑定:

    需求:商品查询controller方法中实现商品查询条件传入。

    实现方法:

    1)在形参中 添加HttpServletRequest request参数,通过request接收查询条件参数。

    2)在形参中让包装类型的pojo接收查询条件参数。

    做法:参数名和包装pojo中的属性一致即可;

    (本例中:<input name="itemsCustom.name" />传递参数  和  ItemsQueryVo属性名itemsCustom一致);

    二、数组绑定:

    需求:商品批量删除,用户在页面选择多个商品,批量删除。

    做法:将页面选择(多选)的商品id,传到controller方法的形参,方法形参使用数组接收页面请求的多个商品id。

    (本例中deleteItems(Integer[] item_id)   item_id用来接收checkbox的name为item_id数组)

    一、二实现如下:

    ItemsController:

     1 // 商品查询
     2     @RequestMapping("/findItems")
     3     public ModelAndView findItems(ItemsQueryVo itemsQueryVo) throws Exception {
     4         
     5         List<ItemsCustom> itemsList = itemsService.findItemsList(itemsQueryVo);
     6         
     7         ModelAndView modelAndView =  new ModelAndView();
     8         modelAndView.addObject("itemsList", itemsList);
     9         modelAndView.setViewName("items/itemsList");
    10         return modelAndView;
    11     }
    12     
    13     // 批量删除 商品信息
    14     @RequestMapping("/deleteItems")
    15     public String deleteItems(Integer[] item_id) throws Exception{
    16         // 调用service批量删除商品
    17         // ...
    18         
    19         for(int id : item_id){
    20             System.out.println("待删除的商品id:---------------->>" + id);
    21         }
    22         
    23         return "success";
    24     }
    View Code

    ItemsQueryVo:

     1 /**
     2  * 商品包装对象
     3  * @author chengyu
     4  *
     5  */
     6 public class ItemsQueryVo {
     7     //商品信息
     8     private Items items;
     9     
    10     //为了系统 可扩展性,对原始生成的po进行扩展
    11     private ItemsCustom itemsCustom;
    12     
    13     //批量商品信息
    14     private List<ItemsCustom> itemsList;
    15     
    16         ...
    17 }
    View Code

    查询和批量删除itemsList.jsp:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
     4 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
     5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     6 <html>
     7 <head>
     8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     9 <title>查询商品列表</title>
    10 <script>
    11 function deleteItems(){
    12     document.itemsForm.action = "${pageContext.request.contextPath }/items/deleteItems.action"; 
    13     document.itemsForm.submit();
    14 }
    15 
    16 function queryItems(){
    17     document.itemsForm.action = "${pageContext.request.contextPath }/items/findItems.action";
    18     document.itemsForm.submit();
    19 }
    20 </script>
    21 </head>
    22 <body> 
    23 <form name="itemsForm" action="" method="post">
    24 查询条件:
    25 <table width="100%" border=1>
    26 <tr>
    27 <td>
    28     商品名称:<input name="itemsCustom.name" />
    29     <input type="button" value="查询" onclick="queryItems()"/>
    30     <input type="button" value="批量删除" onclick="deleteItems()"/>
    31 </td>
    32 </tr>
    33 </table>
    34 商品列表:
    35 <table width="100%" border=1>
    36     <tr>
    37         <td>选择</td>
    38         <td>商品名称</td>
    39         <td>商品价格</td>
    40         <td>生产日期</td>
    41         <td>商品描述</td>
    42         <td>操作</td>
    43     </tr>
    44     <c:forEach items="${itemsList }" var="item">
    45     <tr>
    46         <td><input type="checkbox" name="item_id" value="${item.id}"/></td>
    47         <td>${item.name }</td>
    48         <td>${item.price }</td>
    49         <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
    50         <td>${item.detail }</td>
    51         <td><a href="${pageContext.request.contextPath }/items/editItems.action?id=${item.id}">修改</a></td>
    52     </tr>
    53     </c:forEach>
    54 </table>
    55 </form>
    56 </body>
    57 </html>
    View Code

    三、List绑定:

    需求:通常在需要批量提交数据时,将提交的数据绑定到list<pojo>中,本例子中:批量商品修改,在页面输入多个商品信息,将多个商品信息提交到controller方法中。

    做法:使用List接收页面提交的批量数据,通过Controller形参中包装pojo接收,在包装pojo中定义List<pojo>属性;

    (本例中在ItemsQueryVo中定义itemsList属性接收页面提交的批量商品;status.index定义了下标从0开始;.name/.price..对应了List<ItemsCustom>中ItemsCustom的属性名)

    ItemsController:

     1 // 批量修改商品页面,将商品信息查询出来,在页面中可以编辑商品信息
     2     @RequestMapping("/editItemsQuery")
     3     public ModelAndView editItemsQuery(ItemsQueryVo itemsQueryVo) throws Exception{
     4         List<ItemsCustom> itemsList = itemsService.findItemsList(itemsQueryVo);
     5         ModelAndView modelAndView =  new ModelAndView();
     6         modelAndView.addObject("itemsList", itemsList);
     7         modelAndView.setViewName("items/editItemsQuery");
     8         return modelAndView;
     9     }
    10     
    11     // 批量修改商品提交
    12     // 通过ItemsQueryVo接收批量提交的商品信息,将商品信息存储到itemsQueryVo中itemsList属性中。
    13     @RequestMapping("/editItemsAllSubmit")
    14     public String editItemsAllSubmit(ItemsQueryVo itemsQueryVo) throws Exception {
    15 
    16         return "success";
    17     }
    View Code

    ItemsQueryVo.java:

     1 public class ItemsQueryVo {
     2     //商品信息
     3     private Items items;
     4     
     5     //为了系统 可扩展性,对原始生成的po进行扩展
     6     private ItemsCustom itemsCustom;
     7     
     8     //批量商品信息
     9     private List<ItemsCustom> itemsList;
    10 }
    View Code

    editItemsQuery.jsp:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
     4 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
     5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     6 <html>
     7 <head>
     8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     9 <title>查询商品列表-可批量编辑</title>
    10 <script type="text/javascript">
    11 function editItemsAllSubmit(){
    12     //提交form
    13     document.itemsForm.action="${pageContext.request.contextPath }/items/editItemsAllSubmit.action";
    14     document.itemsForm.submit();
    15 }
    16 function editItemsQuery(){
    17     //提交form
    18     document.itemsForm.action="${pageContext.request.contextPath }/items/editItemsQuery.action";
    19     document.itemsForm.submit();
    20 }
    21 </script>
    22 </head>
    23 <body> 
    24     <form name="itemsForm" action="" method="post">
    25         查询条件:
    26         <table width="100%" border=1>
    27             <tr>
    28                 <td>
    29                     商品名称:<input name="itemsCustom.name" />
    30                 </td>
    31                 <td>
    32                     <input type="button" value="查询" onclick="editItemsQuery()"/>
    33                     <input type="button" value="批量修改提交" onclick="editItemsAllSubmit()"/>
    34                 </td>
    35             </tr>
    36         </table>
    37         商品列表:
    38         <table width="100%" border=1>
    39             <tr>
    40                 <td>商品名称</td>
    41                 <td>商品价格</td>
    42                 <td>生产日期</td>
    43                 <td>商品描述</td>
    44                 <td>操作</td>
    45             </tr>
    46             <c:forEach items="${itemsList }" var="item" varStatus="status">
    47                 <tr>    
    48                     <td><input name="itemsList[${status.index }].name" value="${item.name }"/></td>
    49                     <td><input name="itemsList[${status.index }].price" value="${item.price }"/></td>
    50                     <td>
    51                         <input name="itemsList[${status.index }].createtime" value="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/>
    52                     </td>
    53                     <td><input name="itemsList[${status.index }].detail" value="${item.detail }"/></td>
    54                 </tr>
    55             </c:forEach>
    56         </table>
    57     </form>
    58 </body>
    59 </html>
    View Code

    四、Map绑定:

    在包装类中定义Map对象,并添加get/set方法,action使用包装对象接收。

    包装类中定义Map对象如下:

    1 Public class QueryVo {
    2 
    3 private Map<String, Object> itemInfo = new HashMap<String,Object>();
    4 
    5   //get/set方法..
    6 
    7 }
    View Code

    页面定义如下:

    <tr>
        <td>学生信息:</td>
        <td>
            姓名:<inputtype="text"name="itemInfo['name']"/>
            年龄:<inputtype="text"name="itemInfo['price']"/>
            .. .. ..
        </td>
    </tr>
    View Code

    Controller方法定义如下:

    1 public String useraddsubmit(Model model,QueryVo queryVo)throws Exception{
    2     System.out.println(queryVo.getIteminfo());
    3 }
    View Code
  • 相关阅读:
    CefSharp.v49.0.1浏览器控件完全WPF版,实现禁止弹出新窗口,在同一窗口打开链接,并且支持带type="POST" target="_blank"的链接
    C#动态调用WebService
    WPF实现窗体中的悬浮按钮
    Oracle树结构查询按层级排序
    WPF自定义TabControl样式
    WPF自定义Window窗体样式
    C# 实现图片压缩
    C# 图片反色处理 图片夜间模式
    C#中多线程中变量研究
    EasyNetQ操作RabbitMQ(高级消息队列)
  • 原文地址:https://www.cnblogs.com/tenWood/p/6309002.html
Copyright © 2020-2023  润新知