The Artima Developer Community
Sponsored Link

Java Answers Forum
JSP & Struts - displaying a collection

8 replies on 1 page. Most recent reply: Sep 23, 2003 7:07 AM by Joe Parks

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
Mmutla

Posts: 10
Nickname: mmutla
Registered: Aug, 2003

JSP & Struts - displaying a collection Posted: Sep 19, 2003 6:13 AM
Reply to this message Reply
Advertisement
Can somebody please help me with this question? How can I display elements of a collection one at a time using JSP and Struts?
example:
say I have a collection containing Person objects,
public class Person {
          private String name;
          private String id;
          private String surname;
          // ... and many other variables for this class
      } 

and I want to display the details of each Person on each iteration only. Also is it possible to do this without having Java code in the JSP?

Thanx

Mmutla


Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: JSP & Struts - displaying a collection Posted: Sep 19, 2003 7:11 AM
Reply to this message Reply
Have you seen the <logic:iterate/> tag?

http://jakarta.apache.org/struts/userGuide/struts-logic.html#iterate

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: JSP & Struts - displaying a collection Posted: Sep 19, 2003 7:30 AM
Reply to this message Reply
Joe has given you the right link, but to be little more specific, say if you have an ArrayList of Person objects and in your actiaon class.
List myList = new ArrayList();
//do some operation here to fill in myList with Person Objects
currentSession.setAttribute("resultList",myList);


in your JSP you can do this
<logic:iterate id="element" name="resultList">
   <bean:write name="element" property="name"/>
   <bean:write name="element" property="id"/>
   <bean:write name="element" property="surname"/>
</logic:iterate>


as you can see there is not Java code :-)

Mmutla

Posts: 10
Nickname: mmutla
Registered: Aug, 2003

Re: JSP & Struts - displaying a collection Posted: Sep 22, 2003 2:28 AM
Reply to this message Reply
If I do it the way you are suggesting, <logic:iterate> displays all the elements of the list at once. I want the JSP to display one element at a time, e.g:
If I have a list containing the following Person details:

Joe Maka 19409857
Sam Davies 54009954
Nana Khutso 34355546
Mapule Koto 90096555
I want the user to be able to display Joe's details only on the screen, then do some processing. When they press the next button, it should then display Sam's details and so on for the remaining elements, until all the elements of the list are processed.

Thanx for the replies I got so far.

Mmutla

Mmutla

Posts: 10
Nickname: mmutla
Registered: Aug, 2003

Re: JSP & Struts - displaying a collection Posted: Sep 22, 2003 2:29 AM
Reply to this message Reply
I have seen and used it.

Thanx

Mmutla

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: JSP & Struts - displaying a collection Posted: Sep 22, 2003 3:20 AM
Reply to this message Reply
COuld you post the solution u used?

Mmutla

Posts: 10
Nickname: mmutla
Registered: Aug, 2003

Re: JSP & Struts - displaying a collection Posted: Sep 23, 2003 1:29 AM
Reply to this message Reply
This answer "I have seen and used it" was in response to Joe's comment about <logic:iterate>. I just wanted him to know that I used the iterate tag before and did not achieve the results I wanted. My problem is still not solved, hence my follow up response (which is essentially clarity to my original question) below to your answer :

If I do it the way you are suggesting, <logic:iterate> displays all the elements of the list at once. I want the JSP to display one element at a time, e.g:
If I have a list containing the following Person details:

Joe Maka 19409857
Sam Davies 54009954
Nana Khutso 34355546
Mapule Koto 90096555
I want the user to be able to display Joe's details only on the screen, then do some processing. When they press the next button, it should then display Sam's details and so on for the remaining elements, until all the elements of the list are processed.

Mmutla

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: JSP & Struts - displaying a collection Posted: Sep 23, 2003 2:34 AM
Reply to this message Reply
Here is something you can look into to solve your issue...

http://jsptags.com/tags/navigation/pager/download.jsp

ALso here is a small HOW-TO on this pager.

http://kulkarni_ash.tripod.com/howto/jsptaglib-howto.html

Let me know whether this helped

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: JSP & Struts - displaying a collection Posted: Sep 23, 2003 7:07 AM
Reply to this message Reply
I would actually suggest only sending one Person object to the page. Determine in the Action class which one is desired, and put that in the request.

Otherwise, I would use the JSTL core tags for this:
<%!-- assume that the 'currentId' is a variable in some scope --%>
<c:forEach items="${personCollection}" var="person">
  <c:if test="${person.id eq currentId}">
    <c:out value="${person.firstName}"/>
  </c:if>
</c:forEach> 

It would appear that you could achieve the same using the struts:logic tags (<logic:iterate/> and <logic:equal/>), but I think that the rvalue in the <logic:equal/> tag must be a constant.

Flat View: This topic has 8 replies on 1 page
Topic: please help Previous Topic   Next Topic Topic: Sample Code Required Using jPOS libraries

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use