wood burning stoves 2.0*
The moose likes JSP and the fly likes Using EL to retrive data from Arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Reply locked New topic
Author

Using EL to retrive data from Arraylist

prakash chauhan
Ranch Hand

Joined: Jun 22, 2007
Posts: 81
Hi,
I i have an ArrayList of a class which has its properties (eg. empName)in my servlet , i am adding it to my request
request.setAttribute("LIST",list)

i wanted to go through that arraylist and retieve the property of obects stored in the arraylist using EL or JSTL

-Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Originally posted by prakash chauhan:
i wanted to go through that arraylist and retieve the property of obects stored in the arraylist using EL or JSTL

OK, so starting with iterating through the list, what tags in the JSTL Specification look like they'll do the job?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
prakash chauhan
Ranch Hand

Joined: Jun 22, 2007
Posts: 81
I was thinking about using
${LIST.(somthing here).name } but not able to figure out what should go , and how to loop though,
c:foreach in JSTL also might work but dont know how to get the arraylist elements and call the properties on them
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Yes, <c:forEach> is the appropriate mans to loop through the List.

Look carefully at the attributes of <c:forEach> and you will see that the var attribute allows you to specify the name of the scoped variable that will be set inside the loop to reference each list element.

So you never need to worry about dereferencing the list; <c:forEach> will do it for you.

Now, within the loop, it's a simple matter of using the EL to dereference the properties of each element.
[ November 14, 2007: Message edited by: Bear Bibeault ]
prakash chauhan
Ranch Hand

Joined: Jun 22, 2007
Posts: 81
thanks ,
let me just try it . i m sure i ll be having some more doubts
prakash chauhan
Ranch Hand

Joined: Jun 22, 2007
Posts: 81
What i dont get is this:
i will be using:
But LIST has my ArrayList which has Employee Objects with properties,
which i want to access(display) and number of elements in it is not fixed. What should be my counter? How will i use java.util.Iterator here?

-Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Originally posted by prakash chauhan:
What i dont get is this: i will be using:

No, that is not correct usage.

Consider:


Look up <c:forEach> again and carefully read what the items and var attibutes do.

How will i use java.util.Iterator here?

Once you understand <c:forEach> you will see that you don't have to!
[ November 14, 2007: Message edited by: Bear Bibeault ]
Pranav Pal
Ranch Hand

Joined: Nov 04, 2007
Posts: 74
Originally posted by prakash chauhan:

But LIST has my ArrayList which has Employee Objects with properties,
which i want to access(display) and number of elements in it is not fixed. What should be my counter? How will i use java.util.Iterator here?



You should use <c:forEach> like this
[CODE]<c:forEach var="empElement" items="${LIST} " >
${empElement.empProperty}
</c:forEach>
[CODE]
It iterates through whole the list, so that you need not know number of elements in the list. No need of java.util.Iterator


Hakuna Matata!
prakash chauhan
Ranch Hand

Joined: Jun 22, 2007
Posts: 81
yes finally i got it ..
i was able to iterate through the araylist using <c:forEach>, and found how nice this tag is..i think i shud first try then post a question ,
Thanks for the help Bear and Pranav.
Saurabh Joshi
Ranch Hand

Joined: Nov 15, 2007
Posts: 37
Originally posted by prakash chauhan:
yes finally i got it ..
i was able to iterate through the araylist using <c:forEach>, and found how nice this tag is..i think i shud first try then post a question ,
Thanks for the help Bear and Pranav.
Saurabh Joshi
Ranch Hand

Joined: Nov 15, 2007
Posts: 37
Originally posted by prakash chauhan:
yes finally i got it ..
i was able to iterate through the araylist using <c:forEach>, and found how nice this tag is..i think i shud first try then post a question ,
Thanks for the help Bear and Pranav.


Hi Prakash,

I was also facing a similar issue, can you please help me with this. I am having a array stored in the database and I have to access(display) the array elements on my jsp page.

I was using the following code:


But this is not working,
Please suggest.

Saurabh.
prakash chauhan
Ranch Hand

Joined: Jun 22, 2007
Posts: 81
Your problem seems a bit different from my .
in my case ,here is the scenario.

My table contains Employee data for eg: employeeName with its getters/setters

i have an Action class that fetches these employee records from the database and puts them into an ArrayList and then using setAttributes places it on Request Object . so my request has an ArrayList now say
EmployeeDataList

now i had to access my employee property employeeName so i went through the following code:



and thats how it iterates though my ArrayList.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Saurabh Joshi,
Please ask your separate question in a new thread.

Asking new questions in someone else's thread is called hi-jacking and is a rude thing to do. In this case, since the original poster has already solved his problem, it's not so bad but it still lowers the effectiveness of our search engine if a thread contains more than one subject.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
Don't get me started about those stupid light bulbs.
 
subject: Using EL to retrive data from Arraylist