wood burning stoves 2.0*
The moose likes JSP and the fly likes For Each ArrayList of Object in JSTL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "For Each ArrayList of Object in JSTL" Watch "For Each ArrayList of Object in JSTL" New topic
Author

For Each ArrayList of Object in JSTL

Nestor Alduey
Greenhorn

Joined: Jul 30, 2006
Posts: 26
Hi All,

I want to iterate with an ArrayList of Some other Object that not be strings and stract its fields to show in the jsp.
For example.

<%
List list = (ArrayList) session.getAttribute("default");
for (int i = 0; i < list.size(); i++) {
java.sql.ResultSet rs = (ResultSet) list.get(i); %>
<%=rs.getString(1);%>
<% } %>

I want to do that but with JSTL...

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61227
    
  66

A result set should never be passed to a JSP. Aside from various architectural reasons, it is not a List implementation can cannot be used with the JSTL.

Rather, extract the data from the result set in your model and pass a List to the JSP.

I'm confused as to why you ask how to use an ArrayList when your code shows a ResultSet?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nestor Alduey
Greenhorn

Joined: Jul 30, 2006
Posts: 26
ResultSet is the object of list and I want eval the methods and values of the object of the list...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61227
    
  66

Ah, so your list is composed of multiple result sets?

If so, the same applies. As soon as you get the result set from the DB, copy the data into a List implementation or other appropriate structure and close the result set.

You should never be holding onto a result set in order to pass it to a JSP.
Nestor Alduey
Greenhorn

Joined: Jul 30, 2006
Posts: 26
I told you ResultSet for telling whatever Object thinks a result set like something other Kind of object... I want to extract the fields of those objects that are in a collection.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61227
    
  66

Originally posted by Nestor Alduey:
I told you ResultSet for telling whatever Object thinks a result set like something other Kind of object


Sorry, I understand that English is not your first language, but I cannot figure out what you mean by this. A result set is not a proper collection and should not be sent to a JSP.

I want to extract the fields of those objects that are in a collection.


For objects that are implementations of Map, List or a Java array, the <c:forEach> action is easy to use to iterate over the collection. Let's say that we have a scoped variable named theList that we want to iterate over:



The body of the action will be executed once for each list element, with the scoped variable item referring to the current element.

For a list where the members are themselves another list (which is what I think you might be getting at):



You should have a copy of the JSTL Specification handy which describes the JSTL tags in detail.
[ January 03, 2008: Message edited by: Bear Bibeault ]
Nestor Alduey
Greenhorn

Joined: Jul 30, 2006
Posts: 26
Ok Your understanding me... But Just Wanna Know if I can use one field of that object of that collection

as I said "object.field" I want that object doesn't be a String.
And Yes I'm from Dominican Republic and english is not my first language...

Thanks
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Nestor Alduey:
Ok Your understanding me... But Just Wanna Know if I can use one field of that object of that collection
...


Yes,

Suppose you had a list of beans.
Among the properties in the beans are firstName and lastName.



Does this help?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
To Ben and Bear, I think Nestor should know how MVC works before he can really understand how to use JSTL.


Do you know why this cup is useful? Because it is empty.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61227
    
  66

Everyone creating web applications should understand and employ the MVC pattern.
Nestor Alduey
Greenhorn

Joined: Jul 30, 2006
Posts: 26
Yes It was usefull, thanks a lot...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: For Each ArrayList of Object in JSTL