2
www.abcd.com/user/getuserstats.htm?userId=123123

In this api, the userId gets set to a field named userId in the Action class mapped to this action.

Now,for this

www.abcd.com/user/getuserstats.htm?listOfUsers=123123,456456,789789,42568,58963

I need to know how can we map this list of userIds in an ArrayList defined in the corresponding Action class so that it gets mapped as an ArrayList not as a String.

Note : I don't want to get a a string of userIds and convert it to ArrayList later.I want that the list of the userIds be automatically mapped into a list or an ArrayList. I am sure there must a way to achive that.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Aquarius
  • 421
  • 5
  • 11
  • 2
    Can you show what you already tried? – Pieter Dec 19 '17 at 07:08
  • Possible duplicate of [How to convert comma-separated String to ArrayList?](https://stackoverflow.com/questions/7488643/how-to-convert-comma-separated-string-to-arraylist) – vinS Dec 19 '17 at 07:14
  • hi vinS i have it's not what u r suggesting.pls have a look again. – Aquarius Dec 19 '17 at 07:19
  • hi you can separate string values by , and the convert to array list right i know u mention u don't want to implement this way i just wanted to know the reason. – priyadarshini Dec 19 '17 at 10:07
  • @priyadarshini ... the problem is that if you pass a comma separated string in a GET request,it won't be populated in the action class. Only the first value of the string before the comma will be populated in the respective field value. – Aquarius Dec 26 '17 at 13:32

2 Answers2

1

basically , i found that it can be achieved like this :

www.abcd.com/user/getuserstats.htm?userId=123123&userId=4578&userId=567&userid=987

these params will go into an arraylist already declared in the action class.

Aquarius
  • 421
  • 5
  • 11
1

The values can be submitted in CSV format. Struts2 has a built-in type converter which is able to populate a property that is a collection such as list.

A workaround by using multiple parameters with the same name is possible but it works without using Struts2 type conversion. The last is one of the powerful feature provided by the framework and it is smart less not to use it.

More detailed explanation of this feature is here.

The values should be in the CSV format like in this answer.

That will also give you an idea about type of property which you should bind to the hidden field. For example you can use List or Integer[] for the property that set the values 25, 27, 28.

Struts2 has a built-in converter that converts such values to the list or array automatically.

Roman C
  • 49,761
  • 33
  • 66
  • 176