zoukankan      html  css  js  c++  java
  • SpringMVC入门学习(四)----SpringMVC绑定参数(集合类型绑定)

    1、List集合绑定

    SpringMVC中,如果前端需要传递批量数据时,就可以使用List来接收,此时的 List 集合本身需要放在一个封装对象中,也就是作为一个嵌套的对象类型。List 中可以是基本数据类型,也可以是对象。例如一本书有时不止是一个作者,也可以有多个,所以我们可以在Book类中添加一个Author的集合类,代码如下所示:

    [1]、编写实体类Author:

    public class Author {
        private String name;
        private String age;
        //getter,setter和toString省略
    }
    

    [2]、编写实体类Book:

    public class Book {
        //书的ID
        private Integer id;
        //书名
        private String name;
        //出版社
        private String publisher;
        //图书作者(可以有多位)
        private List<Author> author;
    
        //getter,setter和toString省略
    }
    

    [3]、编写前端提交图书页面index.jsp。注意:绑定List集合时,前端页面中每一组数据的input控件的name属性的格式为:集合名[下标].属性,当请求传递到后端时,处理器适配器会根据name的格式将请求参数解析为相应的List集合。前台的JSP代码如下所示:

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>首页</title>
    </head>
    <body>
    <form action="${pageContext.request.contextPath}/book" method="post">
        图书 ID:<input type="text" name="id"/><br/>
        图书名称:<input type="text" name="name"/><br/>
        出版社:<input type="text" name="publisher"/><br/>
        List类型-作者1名称:<input type="text" name="author[0].name"/><br/>
        List类型-作者1年龄:<input type="text" name="author[0].age"/><br/>
        List类型-作者2名称:<input type="text" name="author[1].name"/><br/>
        List类型-作者2年龄:<input type="text" name="author[1].age"/><br/>
        <input type="submit" value="提交">
    </form>
    </body>
    </html>
    

    [4]、编写Controller类中的具体代码如下:

    //@Controller:表示一个Controller实例,该实例由Spring容器管理
    @Controller
    public class BookController {
        //配置请求的地址
        @RequestMapping(value = "book", method = RequestMethod.POST)
        public String getInfo(Book book) {
            System.out.println("获取到的数据为:ID--" + book.getId()
                    + ",名称--" + book.getName()
                    + ",出版社--" + book.getPublisher()
                    + "
    作者信息--" + book.getAuthor());
            //成功后跳转的页面
            return "success";
        }
    }
    

    [5]、启动Tomcat测试代码,运行后的结果如下所示:

    image

    image

    2、数组绑定

    如果前台传输了多个相同name的数据,比如多选框,SpringMVC可以在处理器方法中使用数组参数接收这些数据。

    [1]、实体类Book:

    public class Book {
        //书的ID
        private Integer id;
        //书名
        private String name;
        //出版社
        private String publisher;
        //图书类型
        private String[] bookType;
        
        //getter,setter和toString省略
    }
    

    [2]、提交数据页面:

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>首页</title>
    </head>
    <body>
    <form action="${pageContext.request.contextPath}/book" method="post">
        图书 ID:<input type="text" name="id"/><br/>
        图书名称:<input type="text" name="name"/><br/>
        出版社:<input type="text" name="publisher"/><br/>
        图书类型 :<input type="checkbox" name="bookType" value="Java">Java
        <input type="checkbox" name="bookType" value="C++">C++
        <input type="checkbox" name="bookType" value="PHP">PHP
        <input type="checkbox" name="bookType" value="Python">Python <br/>
        <input type="submit" value="提交">
    </form>
    </body>
    </html>
    

    [3]、Controller控制器中的代码:

    //@Controller:表示一个Controller实例,该实例由Spring容器管理
    @Controller
    public class BookController {
        //配置请求的地址
        @RequestMapping(value = "book", method = RequestMethod.POST)
        public String getInfo(Book book) {
            System.out.println("图书ID:"+book.getId());
            System.out.println("图书名称:"+book.getName());
            System.out.println("出版社:"+book.getPublisher());
            System.out.println("图书类型:");
            for (String bookType : book.getBookType()) {
                System.out.println(bookType);
            }
            //成功后跳转的页面
            return "success";
        }
    }
    

    [4]、运行结果如下:

    image

    image

    3、Map集合绑定

    SpringMVC也可以绑定Map集合类型,Map的绑定其实和List的绑定是差不多的。虽然Map的使用比较灵活,但是对于这里绑定数据而言就显得乏力了,可维护性比较差,所以因此一般不推荐使用。

    [1]、给上面的Book类添加一个Map属性:

    public class Book {
        //书的ID
        private Integer id;
        //书名
        private String name;
        //出版社
        private String publisher;
        //图书作者(可以有多位)
        private List<Author> author;
        //添加Map集合
        private Map<String, Author> authorMap;
    
        //getter,setter和toString省略
    }
    

    [2]、Controller类中的具体代码如下:

    //@Controller:表示一个Controller实例,该实例由Spring容器管理
    @Controller
    public class BookController {
        //配置请求的地址
        @RequestMapping(value = "book", method = RequestMethod.POST)
        public String getInfo(Book book) {
            System.out.println("获取到的数据为:ID--" + book.getId()
                    + ",名称--" + book.getName()
                    + ",出版社--" + book.getPublisher());
    
            //遍历Map集合
            Map<String, Author> authorMap = book.getAuthorMap();
            Set<Map.Entry<String, Author>> entries = authorMap.entrySet();
            for (Map.Entry<String, Author> entry : entries) {
                System.out.println("键key:"+entry.getKey()+"--作者名称:"
                        +entry.getValue().getName()+"--作者年龄:"+entry.getValue().getAge());
            }
    
            //成功后跳转的页面
            return "success";
        }
    }
    

    [3]、在前端index.jsp页面额外添加如下JSP代码。当想把页面上的批量数据通过Spring MVC转换为Web端的Map类型的对象时,每一组数据的input控件的name属性使用Map名['key值']的形式。当请求传递到Web端时,处理器适配器会根据name的格式将请求参数解析为相应的Map集合。

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>首页</title>
    </head>
    <body>
    <form action="${pageContext.request.contextPath}/book" method="post">
        图书 ID:<input type="text" name="id"/><br/>
        图书名称:<input type="text" name="name"/><br/>
        出版社:<input type="text" name="publisher"/><br/>
        Map类型-作者1名称:<input type="text" name="authorMap['a1'].name"/><br/>
        Map类型-作者1年龄:<input type="text" name="authorMap['a1'].age"/><br/>
        Map类型-作者2名称:<input type="text" name="authorMap['a2'].name"/><br/>
        Map类型-作者2年龄:<input type="text" name="authorMap['a2'].age"/><br/>
        <input type="submit" value="提交">
    </form>
    </body>
    </html>
    

    [4]、将项目部署到Tomcat然后启动测试,效果如下所示:

    image

    image

    作者: 唐浩荣
    本文版权归作者和博客园共有,欢迎转载,但是转载需在博客的合适位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    5 Things Every Manager Should Know about Microsoft SharePoint 关于微软SharePoint每个经理应该知道的五件事
    Microsoft SharePoint 2010, is it a true Document Management System? 微软SharePoint 2010,它是真正的文档管理系统吗?
    You think you use SharePoint but you really don't 你认为你使用了SharePoint,但是实际上不是
    Introducing Document Management in SharePoint 2010 介绍SharePoint 2010中的文档管理
    Creating Your Own Document Management System With SharePoint 使用SharePoint创建你自己的文档管理系统
    MVP模式介绍
    权重初始化的选择
    机器学习中线性模型和非线性的区别
    神经网络激励函数的作用是什么
    深度学习中,交叉熵损失函数为什么优于均方差损失函数
  • 原文地址:https://www.cnblogs.com/tanghaorong/p/14530699.html
Copyright © 2011-2022 走看看