• springmvc接收数组类型参数


    直接在方法上使用List<Bean>报不能实例化错误!

    必须将List<Bean>放在某个对象里作为属性方可接收。具体见如下例子

    前端代码

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>page</title>
    </head>
    <body>
    <h1>数组传参数测试</h1>
    
    <script src="/webpagetest/js/jquery-2.0.3.min.js"></script>
    
    <script type="text/javascript">
    
        function test() {
            $.ajax({
                url:'/webpagetest/pageData',
                data:{
                    'id':1,
                    'order[0].column':'name',
                    'order[0].dir':'asc',
                    'order[1].column':'phone',
                    'order[1].dir':'desc'
                },
                success:function (rt) {
                    alert(rt);
                },
                error:function () {
                    alert('error');
                }
    
            });
        }
    
    
        function test2() {
            $.ajax({
                url:'/webpagetest/pageData2',
                data:{
                    'id':1,
                    'order[0][column]':'name',
                    'order[0][dir]':'asc',
                    'order[1][column]':'phone',
                    'order[1][dir]':'desc'
                },
                success:function (rt) {
                    alert(rt);
                },
                error:function () {
                    alert('error');
                }
    
            });
        }
    </script>
    
    <button onclick="test()">测试</button>
    <button onclick="test2()">测试2</button>
    </body>
    </html>

    后台代码

    @RequestMapping("/pageData")
        @ResponseBody
        public String index(@RequestParam(value="order[0].column",required = false) String order0column,
                            HttpServletRequest request,
                            MyVO vo){
    
            System.out.println("获取单个参数:");
            System.out.println(order0column);
    
            System.out.println("获取所有参数:");
            Map<String,String[]> params =  request.getParameterMap();
            for(Map.Entry<String,String[]> entry:params.entrySet()){
                System.out.println("key:"+entry.getKey()+" value:"+ Arrays.asList(entry.getValue()));
            }
    
            System.out.println("获取VO");
            System.out.println("id:"+vo.getId());
            System.out.println("orders:");
            for(Order o:vo.getOrder()) {
                System.out.println("column:" + o.getColumn()+" dir:"+o.getDir());
            }
    
    
            return "ok";
        }
    
        @RequestMapping("/pageData2")
        @ResponseBody
        public String index(@RequestParam(value="order[0][column]",required = false) String order0column,
                            HttpServletRequest request /*,
                           MyVO2 vo2*/){
    
            System.out.println("获取单个参数:");
            System.out.println(order0column);
    
            System.out.println("获取所有参数:");
            Map<String,String[]> params =  request.getParameterMap();
            for(Map.Entry<String,String[]> entry:params.entrySet()){
                System.out.println("key:"+entry.getKey()+" value:"+ Arrays.asList(entry.getValue()));
            }
    
    //        System.out.println("获取VO2");
    //        System.out.println("id:"+vo2.getId());
    //        System.out.println("orders:");
    //        for(List<String> o:vo2.getOrder()) {
    //            System.out.println("column:" + o.get(0)+" dir:"+o.get(1));
    //        }
    
            return "ok";
        }

    两个实体对象(必须是独立的文件中的类,否则一直报找不到构造函数的错误)

    public class MyVO{
    
        private int id;
    
        private List<Order> order = new ArrayList<Order>();
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public List<Order> getOrder() {
            return order;
        }
    
        public void setOrder(List<Order> order) {
            this.order = order;
        }
    }
    public class Order{
        private String column;
        private String dir;
    
    
        public String getColumn() {
            return column;
        }
    
        public void setColumn(String column) {
            this.column = column;
        }
    
        public String getDir() {
            return dir;
        }
    
        public void setDir(String dir) {
            this.dir = dir;
        }
    }
  • 相关阅读:
    VMWare安装win10提示units specified don’t exist, SHSUCDX can’t install
    WinXP.Http.Post请求错误提示:基础连接已经关闭:发送时发生错误
    如何用PostMan请求WebApi
    无法解决 equal to 运算中 "Chinese_PRC_CI_AS" 和 "Chinese_PRC_CI_AS_WS" 之间的排序规则冲突 解决
    c# Winform PropertyGrid 实现下拉框 多选
    c# Winform GridControl 给列自动生成快捷操作按钮
    Tomcat启动报内存溢出错误:java.lang.OutOfMemoryError: PermGen space异常 解决
    Net Core 项目引用Exceptionless记录使用
    .Net 开源异常日志ExceptionLess搭建
    c# AutoMapper 扩展
  • 原文地址:https://www.cnblogs.com/hdwang/p/9122877.html
Copyright © 2020-2023  润新知