Prasanna
Prasanna

Reputation: 11544

Issue with String array in Spring 3

I am trying to post a String[] from client to Spring 3. In Controller side i have defined the method like this.

@RequestMapping(value = "somemethod", method = RequestMethod.POST)
            public ModelAndView exportSomething(@RequestParam("sentences") String[] sentences) {
                 //.. logic
}

The data that im sending looks like this

sentences: ["a","b,c","d"] 

The problem is in server side the size of the sentences array is 4. It is splitting b and c as two different words.

Is this a issue with Spring or do i need change something the way i pass on the data?

Upvotes: 1

Views: 501

Answers (2)

prashant
prashant

Reputation: 1445

Try sending data in this format.

sentences:"a;b,c;d" Note in this case your delimiter is ; not , So you have sent a string which contains a list of

@RequestMapping(value = "somemethod", method = RequestMethod.POST)
        public ModelAndView exportSomething(@RequestParam("sentences") String sentences) {
            String[] sentenceArray = sentences.split(";");
            for(String tempString:sentenceArray){
               // perform what operation you want to perform
            }

}

you will get an array of size three not four in this case.

The reason why your approach is not working is probably because you have used comma which is the default delimiter for an Array.

Upvotes: 0

Saurabh
Saurabh

Reputation: 7964

Its a know issues I guess with Spring framework. See https://jira.springsource.org/browse/SPR-7963

Upvotes: 3

Related Questions