Spring MVC Controller 接收ajax的数组参数说明

https://blog.csdn.net/u012527870/article/details/72469572

/**
 * Created by xiongyu on 2017/5/12.
 */
$(function () {

  $("#deleteSelect").click(function () {
      .......//省略部分代码
        var res = $.ajax({
            url: "/SpringMybatis/user/deleteBySelective",
            data: {data: chk_value},
              // traditional: true, 这里的取值后面说明
            success: function () {
                console.log(res.responseText)
                loadData();
            }, error: function () {
                console.log(res.responseText)
            }
        })

    });
});
traditional默认是false,如果是false,则传递的参数为:
data[]:1
......
这里设置为true,使传递参数变成:
data:1
......

若传递的数据格式是data[]:1,则RequestParam的value是data[],若传递的数据格式是data:1,则RequestParam的value是data,后面既可以用List接收,也可以用数组接收.

以traditional默认值时为例,controller代码为:

@RequestMapping(value = "deleteBySelective", produces = "text/html;charset=UTF-8;")
    @ResponseBody
    public String deleteBySelective(HttpServletRequest request, Model   model,@RequestParam(value = "data[]")String[] data) {
        System.out.println("request now is here");
        System.out.println(Arrays.asList(data));
        userService.deleteBySelective(Arrays.asList(data));
        return "whatever you like";
    }

相关推荐