aspose file tools*
The moose likes JSP and the fly likes How do I iterate through fields of an object in a JSP? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How do I iterate through fields of an object in a JSP?" Watch "How do I iterate through fields of an object in a JSP?" New topic
Author

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

Stan Lederer
Ranch Hand

Joined: Sep 24, 2012
Posts: 37
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

Joined: Jan 10, 2002
Posts: 61180
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
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

Joined: Sep 24, 2012
Posts: 37
Thanks, Bear and Stefan.

I'll post what I get to work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I iterate through fields of an object in a JSP?