SpringMVC学习笔记(十)——包装类型pojo、数组、集合的参数绑定

博客源码下载:戳我一下

SpringMVC学习笔记汇总:SpringMVC学习笔记汇总

一、包装类型pojo参数绑定

需求:
商品查询controller方法中实现商品查询条件传入。
1、包装类型的pojo

public class ItemsQueryVo {
    //商品信息
    private Items items;
    //为了系统的可扩展性,对原始的po进行扩展
    private ItemsCustom itemsCustom;
        public Items getItems() {
        return items;
    }
    public void setItems(Items items) {
        this.items = items;
    }
    public ItemsCustom getItemsCustom() {
        return itemsCustom;
    }
    public void setItemsCustom(ItemsCustom itemsCustom) {
        this.itemsCustom = itemsCustom;
    }

}

2、controller中的查询方法

    @RequestMapping("/queryItems")
    public ModelAndView queryItems(ItemsQueryVo itemsQueryVo) throws Exception {
        //调用service查找数据库,查询商品列表,这里先使用静态的数据模拟
        List itemsList = itemsService.findItemsList(itemsQueryVo);

        ModelAndView modelAndView = new ModelAndView();
        //相当于request的setAttribute方法
        modelAndView.addObject("itemsList",itemsList);
        //指定视图
        modelAndView.setViewName("items/itemsList");
        return modelAndView;
    }

3、页面代码

<td>
商品的名称:<input name="itemsCustom.name" />
td>

4、包装类型pojo参数绑定总结
itemsCustom和包装pojo中的属性名一致即可。

二、数组类型的参数绑定

需求:
商品批量删除,用户在页面选择多个商品,批量删除。
实现方法:
将页面选择(多选)的商品id,传到controller方法的形参,方法形参使用数组接收页面请求的多个商品id
1、controller中的删除方法

    @RequestMapping("/deleteItems") 
    public String deleteItems(Integer[] items_id) throws Exception {
        //调用service来批量删除商品
        return "success";

    }

2、页面代码

<c:forEach items="${itemsList }" var="item">
<tr>
    <td><input type="checkbox" name="items_id" value="${item.id}" />td>
    <td>${item.name }td>
    <td>${item.price }td>
    <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>td>
    <td>${item.detail }td>

    <td><a href="${pageContext.request.contextPath }/items/editItems.action?id=${item.id}">修改a>td>

tr>
c:forEach>

*3、数组类型参数绑定总结
页面中定义的name属性值和controller中的参数名称相同。

三、List参数绑定

需求:
批量商品修改,在页面输入多个商品信息,将多个商品信息提交到controller方法中。

controller方法定义:
(1)进入批量商品修改页面(页面样式参考商品列表实现)
(2)批量修改商品提交
List接收页面提交的批量数据,通过包装pojo接收,在包装pojo中定义list属性
1、controller中的方法

    //批量修改商品页面,将商品信息查询出来,在页面中编辑商品信息
    @RequestMapping("/editItemsQuery")
    public ModelAndView editItemsQuery(ItemsQueryVo itemsQueryVo) throws Exception {
        //调用service查找数据库,查询商品列表,这里先使用静态的数据模拟
        List itemsList = itemsService.findItemsList(itemsQueryVo);

        ModelAndView modelAndView = new ModelAndView();
        //相当于request的setAttribute方法
        modelAndView.addObject("itemsList",itemsList);
        //指定视图
        modelAndView.setViewName("items/editItemsQuery");
        return modelAndView;
    }
    //批量修改商品提交
    //通过ItemsQueryVo接收批量商品信息,将商品信息存储到itemsQueryVo中的itemsList中
    @RequestMapping("/editItemsAllSubmit")
    public String editItemsAllSubmit(ItemsQueryVo itemsQueryVo) throws Exception {


        return "success";
    }

2、页面代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function editItemsAllSubmit() {
    //提交form
    document.itemsForm.action="${pageContext.request.contextPath }/items/editItemsAllSubmit.action";
    document.itemsForm.submit();
}
function queryItems() {
    //提交form
    document.itemsForm.action="${pageContext.request.contextPath }/items/queryItems.action";
    document.itemsForm.submit();
}
script>
<title>查询商品列表title>
head>
<body> 
<form name="itemsForm" action="${pageContext.request.contextPath }/items/queryItems.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td>
商品的名称:<input name="itemsCustom.name" />
td>
<td>
<input type="button" value="查询" onclick="queryItems()" />
<input type="button" value="批量修改提交" onclick="editItemsAllSubmit()" />
td>
tr>
table>
商品列表:
<table width="100%" border=1>
<tr>
    <td>商品名称td>
    <td>商品价格td>
    <td>生产日期td>
    <td>商品描述td>
    <td>操作td>
tr>
<c:forEach items="${itemsList }" var="item" varStatus="status">
<tr>

    <td><input name="itemsList[${status.index }].name" value="${item.name }" />td>
    <td><input name="itemsList[${status.index }].price" value="${item.price }" />td>
    <td><input name="itemsList[${status.index }].createtime" value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>" />td>
    <td><input name="itemsList[${status.index }].detail" value="${item.detail }" />td>

    <td><a href="${pageContext.request.contextPath }/items/editItems.action?id=${item.id}">修改a>td>

tr>
c:forEach>

table>
form>
body>

html>

3、List类型绑定总结
不是直接在controller方法形参上定义List参数,而是在包装类中定义List属性,在controller方法中用包装类来接收,然后在页面中通过name属性值来调用。
4、部署调试截图
SpringMVC学习笔记(十)——包装类型pojo、数组、集合的参数绑定_第1张图片

四、Map类型参数绑定

Map类型参演书绑定方法和List类型绑定方法类似,也是在包装类中定义Map属性,在controller方法中用包装类来接收,然后在页面中通过name属性值来调用。

你可能感兴趣的:(SpringMVC)