aspose file tools*
The moose likes Struts and the fly likes if tag, collections, and startsWith Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "if tag, collections, and startsWith" Watch "if tag, collections, and startsWith" New topic
Author

if tag, collections, and startsWith

Pierre Coulbert
Greenhorn

Joined: Sep 09, 2008
Posts: 8
Hi all,

I have a requirement to display results of a collection alphabetically, i.e.:

Pets:

A

Aardvark

B

Bat
Beluga

C

Cat
Crayfish

...
I need to access rows of collection petList, test if they begin with a certain letter, and if so display them in the appropriate alphabetical block of the page. I think I need to do something like this for each letter of the alphabet:

<s:iterator value="petList">
<li><s:if test='%{type.toLower.startsWith("a")}'><s:a href="%{url}"><s roperty value="type" escape="false"/></s:a></s:if></li>
</s:iterator>

but it doesn't work (I'm very new to this). How can I do this? Any help gratefully appreciated. I am using Struts 2.
Shikhar Madhok
Ranch Hand

Joined: Dec 18, 2006
Posts: 95
It would be better if you could sort the list and send a sorted list to the iterator tag.

HTH
Shikhar


He who asks a question is a fool for five minutes; he who does not ask a question remains a fool forever - Chinese proverb
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Rule number 1 in building an MVC application is:
If it can be done outside of the JSP, it should be done outside of the JSP.

I'd suggest you do all the heavy lifting for this in your Action class. One approach would be to create a Map<String, List> with the key being the letter of the alphabet, and the value being a List of items that start with that letter. In your JSP, just iterate through the list of Map.Entry objects and then iterate through the list of items in each entry.


Merrill
Consultant, Sima Solutions
Pierre Coulbert
Greenhorn

Joined: Sep 09, 2008
Posts: 8
Thanks, both of you. The Map idea is crafty and I think I'll go that way. Much appreciated.
 
jQuery in Action, 2nd edition
 
subject: if tag, collections, and startsWith