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

 

posted @ 2018-06-01 18:09  追极  阅读(11185)  评论(0编辑  收藏  举报