File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes conditionally generated optionsCollection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "conditionally generated optionsCollection?" Watch "conditionally generated optionsCollection?" New topic
Author

conditionally generated optionsCollection?

Peixiao Lin
Greenhorn

Joined: Oct 04, 2004
Posts: 28
I create a selection list with Struts tag <html:select><htmlptionsCollection>. But now the challenage is how to conditionally generate the optionsCollection? Please refer to following code:



In the code above, form is an ActionForm instance. bean is an instance binded in it. List is a set of instance, whose attribute of name will return a string used as optionsCollection's name. But this doesn't work to me. I thought the index i may not be in the same scope as the <cut> tag?? please help.
[ February 13, 2007: Message edited by: Bear Bibeault ]
Peixiao Lin
Greenhorn

Joined: Oct 04, 2004
Posts: 28
I also tried to use <bean:write> tag as following



but this failed because of illegal quote nesting. any thoughts?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65

You cannot nest custom actions in this way (using a custom action as the attribute of another).

What version of JSP are you using? (forum instructions direct that you should have included this information in your post).

If JSP 2.0, you can omit the <cut> and just use the EL expression. If not, well you'll need to come at the problem differently.
[ February 13, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Peixiao Lin
Greenhorn

Joined: Oct 04, 2004
Posts: 28
My bad...I am using JSP 1.2 so maybe I have to think about this problem differently. what about put some conditional tag between <html:select>..something like this

<html:select ...>
<logic:equal>
<htmlptionsCollection ... />
</logic:equal>
</html:select>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65

With JSP 1.2 you are either going to have to punt back to scriplets (yuck) or find some Struts-enabled way of doing this.

I'm moving this on to the Struts forum at this point where the Struts-savvy can have a look at this.
Peixiao Lin
Greenhorn

Joined: Oct 04, 2004
Posts: 28
Plus, if I use scriptlet here, I can not grab the index i into the scriptlet code. correct me if I am wrong. thanks
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I think you need to look a little closer at the documentation for the html ptionsCollection tag: http://struts.apache.org/1.1/userGuide/struts-html.html#optionsCollection

You generally do not use an iterate tag with an optionsCollection tag. The "property" attribute specifies the property on your form that returns a Collection and the optionsCollection tag takes care of the iteration.

- Brent
Peixiao Lin
Greenhorn

Joined: Oct 04, 2004
Posts: 28
that's right. The point here actually is how to dynamically change the property attribute value in <html ptionsCollection> tag. Any thoughts please? thanks.
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
It doesn't make sense to have select tag within iterate and optionsCollection.
I believe you want something like this


But I am confused by your naming conventions it's better you name the beans appropriately.
[ February 13, 2007: Message edited by: Purushothaman Thambu ]
Peixiao Lin
Greenhorn

Joined: Oct 04, 2004
Posts: 28
yeah, the question itself is confusing and I didn't ask in a clear way. I will try my best to clarify it. I am writing a table and trying to create rows dynamically.

<table>
<logic:iterate name="earth" property="northAmerican.countries" indexId="i" type="com.test.country">
<tr>
<td>
<html:select name="earth" property='<%="northAmerican.countries["+i+"].name"%>'/>
<html ptionsCollection name="options" property="countries">
</html:select>
</td>
</tr>
<tr>
<td>
<html:select name="earth" property='<%="northAmerican.countries["+i+"].state"%>'/>
<html ptionsCollection name="options" property="USA">
</html:select>
</td>
</tr>
</logic:iterate>
</table>

so here, "options" has a set of collections properties that represents all north american countries and all states for each countries. for example, countries is a collection of all coutries' names and USA is a collection of all states' name in the USA, and so on. "countries" is a list that has a set of country instance in it. Each "com.test.country" instance has name string and state string in it. User shoud be able to specify a country and a state in that country in this table. therefore, second selection list, which is states, will depend on first country choice. but the problem is how I can chcnage the property value in second <html ptionsCollection> tag instead of hardcode "USA"? or how I can pass the first choice value into that property attribute? please help.
Peixiao Lin
Greenhorn

Joined: Oct 04, 2004
Posts: 28
I thought it might be resolved by nested tag? Not sure, will do some research.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Your use of the html ptionsCollection tag inside of an html:iterate tag still seems wrong to me. Do you have a Select box for every country our just one Select box? It seems like what you really want are dependant select boxes. The first select box would contain countries (Canada, Mexico, USA) and the second select box would be updated based on the selection in the first select box. If this is what you what there are threads here or resources on the Internet to help you.

- Brent
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: conditionally generated optionsCollection?