Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequestParam get array from select option html form

Tags:

spring-mvc

I am trying to get multiple values from a multiple select option box in html.

I get in to my controller:

/test?t=1&t=2&t=3

In the controller, I try to get the int array:

@RequestParam(value = "t", required = true) int[] t

But when I check it using:

t.length

I only see 1, which means Spring only gets 1 parameter, but I expected 3. Anybody have any idea?

like image 544
mamruoc Avatar asked Jan 19 '26 21:01

mamruoc


2 Answers

I don't think that spring will convert a parameter array to a specific type other than String, so you should try the following:

@RequestParam(value = "t", required = true) String[] t

and then use Integer.parseInt() to convert the String to an int.

like image 130
John Farrelly Avatar answered Jan 21 '26 11:01

John Farrelly


This is working as expected with Spring 3.2 version. I have a method:

@RequestMapping(value = "/blueprint", method = RequestMethod.GET)
public ModelAndView blueprint(@RequestParam(value = "blueprints", required = false) int[] blueprints)

and when using

http://localhost:9000/blueprint?blueprints=2&blueprints=1

or

http://localhost:9000/nbu-portal-webapp/blueprint?blueprints=1,2

the values are being converted to the correct int array.

like image 29
user2121434 Avatar answered Jan 21 '26 10:01

user2121434



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!