码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc接收数组类型参数

时间:2018-06-01 19:13:10      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:system   tor   func   error:   对象   UI   ams   IV   ring   

直接在方法上使用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;
    }
}

 

springmvc接收数组类型参数

标签:system   tor   func   error:   对象   UI   ams   IV   ring   

原文地址:https://www.cnblogs.com/hdwang/p/9122877.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!