phoenix
phoenix

Reputation: 995

How to send string[] to the spring controller?

I have the following JQuery code which sends some request parameters to my Spring MVC controller. For some parameters I should get multiple values.

 $('#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: ['[email protected]', '[email protected]'],
         reportSource: data, //Base64 data
         reportSourceType: 'adhoc',
         reportName: 'DataQualityApp'
     });
     event.preventDefault();
 });

When the tb-email is clicked, request is submitted to some controller which is saved in the DashboardGlobals variable.

At the server side I have written the following Java code to get the multiple values for the parameters formats and recipients.

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

I checked the formats and recipients length which is 1.

I even tried to get the values using

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

Still I am getting single values in the array. The length is still one?

What is going wrong?

Upvotes: 0

Views: 418

Answers (1)

user2575725
user2575725

Reputation:

You may try this, spring controller can take csv as array or list:

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

OR

$.post(window.DashboardGlobals.sendMail, {
     formats: 'png,pdf',
     recipients: '[email protected],[email protected]',
     ...
});

Upvotes: 1

Related Questions