• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

if tag, collections, and startsWith

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, both of you. The Map idea is crafty and I think I'll go that way. Much appreciated.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic