• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

if tag, collections, and startsWith

 
Pierre Coulbert
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be better if you could sort the list and send a sorted list to the iterator tag.

HTH
Shikhar
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pierre Coulbert
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, both of you. The Map idea is crafty and I think I'll go that way. Much appreciated.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic