• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to display comma separated values on jsp using jstl  RSS feed

 
Vinee Vijay
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to display comma separated values in a single <td> tag of a jsp. The data comes from a List of beans. I have to pick up the 2nd item in each bean and display the values separated by commas in a straight line. The data cannot be wrapped and I am not allowed to use java scriptlets anywhere in the jsp. But if two of the beans in the List are the same then the value should be displayed only once ie., only unique values can be displayed.

Currently I have used the following:
1)a c:foreach tag to iterate thru the list of beans
2)A c:choose tag inside the c:foreach. A c:when tag which checks whether the current item is the last one using the 'last' attribute. If the data item is the last one then it prints it without a comma following it. A ctherwise tag which prints the data item followed by a comma. I don't know whether this would work. Even if this works how do I eliminate duplicate values?

Is their a simpler and better way of doing this.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
144
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The task of assembling the list to be displayed with the elimination of duplicate values is a procesing task that I would deem too complicated for the JSP and I would have it take place in the controller prior to forwarding to the JSP.
[ August 06, 2006: Message edited by: Bear Bibeault ]
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have control over the population of this list?

Assembling the list of unique values in java code would be as simple as using a Set instead of a List. That way you get uniqueness for "free".

If order doesn't matter, use a HashSet.
If the records are sorted, use a TreeSet.
 
Vinee Vijay
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, thanks a lot both of you. We have decided to eliminate the duplicate values using java. And for the display I used a c:choose, c:when tag nested inside a c:foreach which made the display logic very simple.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
144
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vinee Vijayan:
... which made the display logic very simple.


You will thank yourself long and often whenever you can do this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!