【问题标题】:How to send string[] to the spring controller?如何将字符串 [] 发送到弹簧控制器?
【发布时间】:2014-09-03 06:41:37
【问题描述】:

我有以下 JQuery 代码,它将一些请求参数发送到我的 Spring MVC 控制器。对于某些参数,我应该得到多个值。

 $('#tb-email').click(function(event) {
     var base, data, formats, recipients, reportSource, reportSourceType;
     if ($(this).parent('li').hasClass('disabled')) {
         return false;
     }
     base = "<base href=\"" + window.location.protocol + "//" + window.location.host + window.DashboardGlobals.baseUrl + "\">";
     data = $('html').clone().find('script').remove().end().find('nav').remove().end().find('#dashboardCanvas').removeClass('dashboardCanvas').end().find('head').prepend(base).end().html();
     data = encodeURIComponent(Base64.encode('<html>' + data + '</html>'));
     $.post(window.DashboardGlobals.sendMail, {
         formats: ['png', 'pdf'],
         recipients: ['abc@xyz.com', 'xyz@abc.com'],
         reportSource: data, //Base64 data
         reportSourceType: 'adhoc',
         reportName: 'DataQualityApp'
     });
     event.preventDefault();
 });

当点击tb-email 时,请求被提交给保存在DashboardGlobals 变量中的某个控制器。

在服务器端,我编写了以下 Java 代码来获取参数格式和接收者的多个值。

public @ResponseBody String process(@RequestParam("formats") String[] formats, @RequestParam("recipients") String[] recipients, @RequestParam("reportSource") String reportSource, @RequestParam("reportSourceType") String reportSourceType, HttpServletRequest request) {
        ...Some Processing....
    return null;
}

我检查了formatsrecipients 的长度是1。

我什至尝试使用

获取值
String[] formats = request.getParameterValues("formats");
String[] recipients = request.getParameterValues("recipients");

我仍然在数组中获得单个值。长度还是一?

出了什么问题?

【问题讨论】:

  • 您是否尝试过检查formats 的值是否包含数组。
  • 使用数据:JSON.stringify(yourarray) 然后在控制器中使用JSONArray 而不是String[]
  • @Kerppag 它由单个值组成
  • sysout 时能否发布输出 request.getParamterValues("formats");

标签: java javascript jquery http-request-parameters


【解决方案1】:

你可以试试这个,spring控制器可以将csv作为数组或列表:

$.post(window.DashboardGlobals.sendMail, {
     formats: ['png', 'pdf'].join(","),//<-- create csv string
     recipients: ['abc@xyz.com', 'xyz@abc.com'].join(","),//<-- create csv string
     ...
});

$.post(window.DashboardGlobals.sendMail, {
     formats: 'png,pdf',
     recipients: 'abc@xyz.com,xyz@abc.com',
     ...
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 2017-06-24
    • 1970-01-01
    相关资源
    最近更新 更多