This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I iterate through fields of an object in a JSP?

 
Stan Lederer
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a servlet and a JSP. The servlet will query a Lucene index and retrieve an object of type SolrDocument (documented here.) My goal is to have the JSP iterate over the fields of the object and display them. Here's a Java class that displays a few fields of the object. The code works but it's doing the printing that I can't figure out how to get the JSP to do:


Note that while resultId and title are strings, authors is an array list. If it's easier for the JSP I can have the servlet concatenate the authors field list elements into a single string.

I've read about the JSTL <c:forEach> tag but it iterates over arrays and collections.

What do I pass to the JSP and how would the JSP iterate over the fields of what I pass it?

Thanks very much.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan Lederer wrote:I've read about the JSTL <c:forEach> tag but it iterates over arrays and collections.

Which includes implementations of List and Map.
 
Stefan Evans
Bartender
Posts: 1712
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And looking at the class via the javadoc, these things actually do implement List and Map interfaces.

So you should easily be able to use a <c:forEach> loop with a SolrDocumentList and a SolrDocument.

More proof that these classes are JSTL friendly from the javadoc:

There's a handy helper method that they've put there for this exact purpose!

 
Stan Lederer
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Bear and Stefan.

I'll post what I get to work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic