Forums Register Login

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

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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!

+Pie Number of slices to send: Send
Thanks, Bear and Stefan.

I'll post what I get to work.
Whatever you say buddy! And I believe this tiny ad too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2855 times.
Similar Threads
Read Excel Sheet Using Java
Iterator problem
JSP, Solr - How to get fields from an Interator
can' t understand iterator
JSTL for loop error
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:37:13.