• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

For Each ArrayList of Object in JSTL

 
Nestor Alduey
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Nestor Alduey
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Hongli Li
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Ben and Bear, I think Nestor should know how MVC works before he can really understand how to use JSTL.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everyone creating web applications should understand and employ the MVC pattern.
 
Nestor Alduey
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes It was usefull, thanks a lot...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic