• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Struts 2 Select Tag for dynamic values

 
Abdul Mujeeb Md
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have an ArrayList which contains String[] array objects. How do i display a selected object using Struts 2 select tag?

Eg:



How do i access the 1, Apple using from the dropdown.

Thanks.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abdul I don't think that code will compile. You are storing single quoted Strings arrays into an uninitialized one dimensional String array. And there is a logical mistake too, you are assigning all the values on the same index location i.e. 0. Then you have ArraList about which we don't know if it is a property inside your action or something else. Please clarify all this first...
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Abdul I don't think that code will compile. You are storing single quoted Strings arrays into an uninitialized one dimensional String array. And there is a logical mistake too, you are assigning all the values on the same index location i.e. 0. Then you have ArraList about which we don't know if it is a property inside your action or something else. Please clarify all this first...


Hi Ankit, yes code gives compile time error.
 
Abdul Mujeeb Md
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ankit,

Extremely sorry. That was a typo. I have written it as an example. The code has compiled successfully and i am able to access the values of the String Array using <s:property> tag.

The exact representation goes this way.



I am unable to access the same in the drop down. I hope i made myself clear.

Thanks
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Convert your array to list using



above code in your action class. Make arrayList global variable. Now your Jsp code looks like..





 
Abdul Mujeeb Md
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nishan,

Thanks for your response.

That works fine for a single String Array.



How do we extend it to work for the above scenario. What if i have to list the values Red, Yellow, Green in the drop down and access the selected value in the Action using its key i.e; 1 or 2 or 3

Thanks.
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to use <s:select with multiple list use ><s:merge...> tag..

refer below link for more details ...

Struts 2 merge

or

Java Ranch


 
Abdul Mujeeb Md
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nishan,

You mean we have to have the String Array converted to a List to be accessed in the Struts Tag? Isn't there any other way we can achieve it?
I am using a Method which returns me an ArrayList(which contains String Array as its objects). The back end logic is hidden from me, what i only have is an ArrayList which has got the data in the said format. As I have only a single getter/setter method for the above ArrayList, i need to iterate it and fetch the data from it.




Now i have to only use arlFruitInfo to access the data from it.

How can this be done in the select drop-down.

Thanks.
 
Abdul Mujeeb Md
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is there a way to use the select tag to display the bean contents, contained in a collection?

Eg:

How do we access "arlUserInfo" using Struts 2 Select option?

Thanks.
 
Mike Cobelmach
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must have appropriate getter for list arlUserInfo. For access this in JSP use attributes list, listkey and listvalue of select tag.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic