File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes java 1.5 list iteration using jstl tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "java 1.5 list iteration using jstl tag" Watch "java 1.5 list iteration using jstl tag" New topic
Author

java 1.5 list iteration using jstl tag

krish chaitu
Ranch Hand

Joined: Oct 09, 2007
Posts: 32
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

Joined: Jan 10, 2002
Posts: 60050
    
  65

<c:forEach> will iterate over anything that implements List.
[ October 23, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
krish chaitu
Ranch Hand

Joined: Oct 09, 2007
Posts: 32
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

Joined: Jan 10, 2002
Posts: 60050
    
  65

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

Joined: Oct 09, 2007
Posts: 32
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

Joined: Jan 10, 2002
Posts: 60050
    
  65

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

Joined: Apr 20, 2002
Posts: 510
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

Joined: Jan 10, 2002
Posts: 60050
    
  65

If you are unfamiliar with using the EL, the EL chapter of the JSP spec is a must-read.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

In this case, the JSTL spec is a must-read too. You'll find both here:
  • JSP 2.0
  • JSTL 1.1


  • [My Blog]
    All roads lead to JavaRanch
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: java 1.5 list iteration using jstl tag
     
    Similar Threads
    Need help regarding s:select tag list
    struts Action for asynchronous HTTP processing
    retaining selected values.....list box
    scriptlet processing query in JSP
    How to reterive table row value (JSP )from the action class