Java通过jquery传递数组

时间:2014-01-18 09:28:27

标签: java jquery spring-mvc

我的行动如下

 @RequestMapping(value = "info_pure", method = RequestMethod.GET)
    public ModelAndView GetInfo(String ser_name, String[] p_input_week_days, int p_input_month_day) {
        ModelAndView mav = new ModelAndView(JspView.PureInfoAup);       
        String aaa = request.getParameter("p_input_week_days");

我通过jquery ajax向此操作发送get请求。

 var myarray = ['Element 1', 'Element 2', 'Element 3'];

        var dataobject = {
            ser_name: p_ser_name.trim(), p_input_week_days: myarray, p_input_month_day: p_month_day
        };

        $.ajax({
            url: '../info_pure',
            data: dataobject,
            type: 'GET',
            success: function(data) { /// go forward

但是当请求发送时给我一个错误。它说我的数组参数为空

这是我在firebug上的结果

enter image description here

2 个答案:

答案 0 :(得分:1)

使用Spring MVC @RequestParam注释。

@RequestMapping(value = "info_pure", method = RequestMethod.GET)
public ModelAndView GetInfo(@RequestParam("p_input_week_days[]") String[] days) {   
    String[] aaa = days;

更多详情:http://docs.spring.io/spring/docs/4.0.0.RC1/spring-framework-reference/html/mvc.html#mvc-ann-requestparam

答案 1 :(得分:0)

我使用Google Gson库解决了这个问题。

像这样更新您的控制器方法

 @RequestMapping(value = "info_pure", method = RequestMethod.GET)
    public ModelAndView GetInfo(String ser_name, String p_input_week_days, int p_input_month_day) {

      Gson gson=new Gson();
      String[] weeks=gson.fromJson(p_input_week_days, String[].class);
      // ....          

像这样更新你的Ajax调用

    var myarray = ['Element 1', 'Element 2', 'Element 3'];

    var dataobject = {
        ser_name: p_ser_name.trim(), p_input_week_days: JSON.stringify(myarray), p_input_month_day: p_month_day
    };

    $.ajax({
        url: '../info_pure',
        data: dataobject,
        type: 'GET',
        success: function(data) { /// go forward