zoukankan      html  css  js  c++  java
  • springMvc--接受请求参数

    作者:liuconglin

    接收基本类型

    表单:

    <h1>接受基本类型参数到Controller</h1>
           <form action="/param/test" method="post">
               用户编号:<input type="text" name="id"/>
               用户名:<input type="text" name="name"/>
               <input type="submit" value="提交"/>
           </form>

    controller:

    /**
         * 接受基本类型参数
         */
        @RequestMapping("/test")
        public String test(Integer id,String name){
            System.out.println(id);
            System.out.println(name);
            return "index";
        }

    我的表单是post提交的,开始是发生了乱码问题,那么如何解决post乱码问题呢?

      需要在web.xml文件中配置一个字符过滤器

    <!-- springMvc处理post提交中文乱码 -->
      <filter>
          <filter-name>characterEncoding</filter-name>
          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
          <!--  指明定编码规则 -->
          <init-param>
              <param-name>encoding</param-name>
              <param-value>UTF-8</param-value>
          </init-param>
          <init-param>
              <param-name>forceEncoding</param-name>
              <param-value>true</param-value>
          </init-param>
      </filter>
      <filter-mapping>
          <filter-name>characterEncoding</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>

    接收对象类型:

    实体类:user和order是一对多的关系

    public class User {
            private String id;  
            private String name;
            private Order order;
                   
                    ........        
    }    
    public class Order {
        private Integer no;
        private String name;
            .......
    )

    1)接收有关系的对象

    表单:

    <h1>接受对象类型参数到Controller</h1>
           <form action="/param/test1" method="post">
               用户编号:<input type="text" name="id"/>
               用户名:<input type="text" name="name"/>
               订单编号:<input type="text" name="order.no"/>
               订单名称:<input type="text" name="order.name"/>
               <input type="submit" value="提交"/>
           </form>

     controller:

    /**
         * 接受对象类型参数
                通过user中的关系属性,获得order
         */
        @RequestMapping("/test1")
        public String test1(User user){
            System.out.println(user.getid());
            System.out.println(user.getOrder().getNo());
            return "index";
        }        

    2)接收多个对象

    如果是没有关系的实体类,需要把实体类封装在一个DTO中:

    BeanVO:

    /**
    *user和order数据传输类
    */
    public class BeanVO {
    
        private User user;
        private Order order;
            ......
    }

     controller:

    /**
         * 接受多个对象类型参数
         *  如果多个pojo之间没有关系,也可以采用这个方法
         */
        @RequestMapping("/test2")
        public String test1(BeanVO beanVO){
            System.out.println(beanVO.getUser());
            System.out.println(beanVO.getOrder());
            return "index";
        }

    接收数组类型:

    springMvc支持接收数组类型

    表单:

    <h1>接受数组类型参数到Controller</h1>
           <form action="/param/test3" method="post">
               str[0]:<input type="text" name="str"/>
               str[1]:<input type="text" name="str"/>
               str[2]:<input type="text" name="str"/>
               <input type="submit" value="提交"/>
           </form>

    controller:

    /**
         * 支持接收接受数组类型
         * @requestMapping 中
         *         属性二:method=RequestMethod.POST
         * 注意:
         *     一旦方法声明的@requestMapping注解中加入method属性,该方法只能被method属性中声明的提交方式访问
         *  默认支持所有提交方式
         */
        @RequestMapping(value="/test3",method=RequestMethod.POST)
        public String test2(String[] str){
            for (String string : str) {
                System.out.println(string);
            }
            return "index";
        }

    接收集合类型:

    springMvc不支持直接接受集合类型,需要定义一个DTO数据传输类:

    public class CollectionVO {
        private List<String> list;
        private List<User> users;
        private Map<String,User> maps;
            ......
    }

    接下来分别测试

    1) list集合泛型是基本类型,例如:list<String>

    表单:

    <h1>接受集合类型泛型是基本类型参数到Controller</h1>
           <form action="/param/test4" method="post">
               list[0]:<input type="text" name="list"/>
               list[1]:<input type="text" name="list"/>
               list[2]:<input type="text" name="list"/>
               <input type="submit" value="提交"/>
           </form>

    controller:

    /**
         * 接受集合类型参数 List<String>
         *     注意:springmvc 不支持集合类型参数接受
         *  解决办法:
         *      封装接收
         */
        @RequestMapping("/test4")
        public String test3(CollectionVO collectionVO){
            for (String string : collectionVO.getList()) {
                System.out.println(string);
            }
            return "index";
        }

    2)list集合泛型对象类型,例如:List<User> users

    form表单:

    <h1>接受集合泛型User类型参数到Controller</h1>
           <form action="/param/test5" method="post">
               users[0].id:<input type="text" name="users[0].id"/>
               users[1].id:<input type="text" name="users[1].id"/>
               users[2].id:<input type="text" name="users[2].id"/>
               <input type="submit" value="提交"/>
           </form>

    controller: 接受到的view数据,会被封装成3个User

    /**
         * 接受集合类型参数 List<User>
         *     注意:springmvc 不支持集合类型参数接受
         *  解决办法:
         *      封装接收
         */
        @RequestMapping("/test5")
        public String test4(CollectionVO collectionVO){
            for (User user : collectionVO.getUsers()) {
                System.out.println(user);
            }
            return "index";
        }

    3)Map集合: Map集合中封装的是 Map<String,User> maps

    form表单:

    <h1>接受Map集合泛型String,User类型参数到Controller</h1>
           <form action="/param/test6" method="post">
               maps:<input type="text" name="maps['123'].id"/>
               maps:<input type="text" name="maps['456'].id"/>
               maps:<input type="text" name="maps['789'].id"/>
               <input type="submit" value="提交"/>
           </form>

    controller: 

    /**
         * map集合参数接受  Map<String,User> maps
         */
        @RequestMapping("/test6")
        public String test5(CollectionVO collectionVO){
            
            for (Map.Entry<String, User> map : collectionVO.getMaps().entrySet()) {
                System.out.println(map.getKey()+"   ------>"+map.getValue());
            }
            
            return "index";
        }
  • 相关阅读:
    Spring Boot 2 快速教程:WebFlux Restful CRUD 实践(三)
    Spring Boot 2 快速教程:WebFlux 快速入门(二)
    ES 集群上,业务单点如何优化升级?
    Spring Boot 2.x 系列教程:WebFlux 系列教程大纲(一)
    泥瓦匠想做一个与众不同的技术"匠"
    java编程行业微信群,无论新手老手欢迎加入,会一直更新
    Spring Boot 2.x 系列教程:WebFlux REST API 全局异常处理 Error Handling
    解决方案:如何防止数据重复插入?
    阿里 Java 手册系列教程:为啥强制子类、父类变量名不同?
    品阿里 Java 开发手册有感
  • 原文地址:https://www.cnblogs.com/lxl57610/p/8241932.html
Copyright © 2011-2022 走看看