aspose file tools*
The moose likes Struts and the fly likes Using Struts 2 Select Tag for dynamic values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Using Struts 2 Select Tag for dynamic values" Watch "Using Struts 2 Select Tag for dynamic values" New topic
Author

Using Struts 2 Select Tag for dynamic values

Abdul Mujeeb Md
Greenhorn

Joined: Aug 25, 2008
Posts: 26
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

Joined: Aug 03, 2008
Posts: 9293
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684

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.


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Abdul Mujeeb Md
Greenhorn

Joined: Aug 25, 2008
Posts: 26
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

Joined: Sep 07, 2008
Posts: 684

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

Joined: Aug 25, 2008
Posts: 26
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

Joined: Sep 07, 2008
Posts: 684

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

Joined: Aug 25, 2008
Posts: 26
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

Joined: Aug 25, 2008
Posts: 26
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

Joined: Jun 17, 2009
Posts: 19
You must have appropriate getter for list arlUserInfo. For access this in JSP use attributes list, listkey and listvalue of select tag.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Struts 2 Select Tag for dynamic values