• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java 1.5 list iteration using jstl tag

 
krish chaitu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

iam using struts & jstl as my framework to develop web pages, i have a action class that will call one DAO & that DAO returns a List<DTO>,in my action class i have like

List<DTO> list=dao.getList();
so here iam getting the generic list as return,so i have to display this list in a jsp page while looping through the list

can any one please help me how i can iterate it in jsp page using jstl tag.
i know that we have c:forEach,c:Iterator,but not sure how to iterate java 1.5 list.



Thanks

krish
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<c:forEach> will iterate over anything that implements List.
[ October 23, 2007: Message edited by: Bear Bibeault ]
 
krish chaitu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for your reply,but still iam confused of using the c:forEach tage,
as i have a List<DTO> list=dao.getList();
so as per jstl c:forEach syntax

<c:forEach var="x" items="..." > ... </c:forEach>
what value do i need to give for var,items,how will i get the list in this tag,
its very confusing,

so please can anyone let me know in detail how to implement this tag for my requirement

Thanks
krish
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before forwarding to the JSP, your action class must create a scoped variable using the list. Something perhaps like:



Then in the JSP you can use:

 
krish chaitu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
thank you very much for your reply.
my list is generally a java1.5 list,like List<DTO> & from this list i need to get the DTO objects ,so will <c:forEach will only get the values from the DTO .in general we will have fields & corresponding setter & getters.

in normal
we will do like for(DTO dto:List list) & it will get the dto objects,

So can anyone let me know,how will c:forEach approach my requirement.

Thanks
Krish
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Within the body of the forEach, the scoped variable item will be each element of the List in turn. That means, since your list is a List<DTO>, that each item will be a DTO instance.
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should look on the web for an example of the <c:forEach ...> tag. Find one that uses the EL and dot notation to access object properties.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are unfamiliar with using the EL, the EL chapter of the JSP spec is a must-read.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case, the JSTL spec is a must-read too. You'll find both here:
  • JSP 2.0
  • JSTL 1.1
  •  
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic