zoukankan      html  css  js  c++  java
  • springmvc注解开发参数类型绑定

    一、包装类型POJO绑定

    什么是包装类型:多个POJO类型的组合

    如:一个学生(Students)所在的班级(Classes)中各科(Source)成绩(Score),可能会涉及到多个POJO这样,就需要使用包装类型的POJO

    代码:

    StudentsQueryVo{
        private Students stutent;
        private Classes class;
        private Source source;
        private Score score;
    }

    具体用法:

    1、定义一个包装类型POJO

    public class ItemsQueryVo {
        //为了系统 可扩展性,对原始生成的po进行扩展
        private ItemsCustom itemsCustom;

    2、页面定义

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

    3、controller

    public ModelAndView queryItems(ItemsQueryVo itemsQueryVo) throws Exception

    二、集合类型绑定

      Ⅰ、数组类型

        ①、页面定义

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

        ②、controller方法:

        // 批量删除 商品信息
        @RequestMapping("/deleteItems")
        public String deleteItems(Integer[] items_id) throws Exception {

      Ⅱ、List参数绑定

        ①、包装类型
          使用List接收页面提交的批量数据,通过包装pojo接收,在包装pojo中定义list<pojo>属性

    public class ItemsQueryVo {
        
        //为了系统 可扩展性,对原始生成的po进行扩展
        private ItemsCustom itemsCustom;
        
        //批量商品信息
        private List<ItemsCustom> itemsList;

        ②、页面定义 

    <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="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
        <td><input name="itemsList[${status.index }].detail" value="${item.detail }"/></td>
    </tr>
    </c:forEach>

        ③、controller方法

        // 批量修改商品提交
        // 通过ItemsQueryVo接收批量提交的商品信息,将商品信息存储到itemsQueryVo中itemsList属性中。
        @RequestMapping("/editItemsAllSubmit")
        public String editItemsAllSubmit(ItemsQueryVo itemsQueryVo)
                throws Exception {

      Ⅲ、map参数绑定

        ①、定义包装类

    private Map<String, Object> itemInfo = new HashMap<String, Object>();

        ②、页面定义

    姓名:<inputtype="text"name="itemInfo['name']"/>
    年龄:<inputtype="text"name="itemInfo['price']"/>

        ③、controller方法

    public String xxxsubmit(QueryVo queryVo)throws Exception{
  • 相关阅读:
    【Elasticsearch 技术分享】—— ES 常用名词及结构
    【Elasticsearch 技术分享】—— Elasticsearch ?倒排索引?这都是什么?
    除了读写锁,JUC 下面还有个 StampedLock!还不过来了解一下么?
    小伙伴想写个 IDEA 插件么?这些 API 了解一下!
    部署Microsoft.ReportViewe
    关于TFS强制undo他人check out
    几段查看数据库表占用硬盘空间的tsql
    How to perform validation on sumbit only
    TFS 2012 Disable Multiple Check-out
    在Chrome Console中加载jQuery
  • 原文地址:https://www.cnblogs.com/WarBlog/p/15020462.html
Copyright © 2011-2022 走看看