?
Solved

Iterating a List with Struts

Posted on 2005-03-25
7
Medium Priority
?
1,192 Views
Last Modified: 2013-11-24
Hi,
   I've finally got Struts to play nice and I've even gotten Hibernate to work to. Now I have a nice list of objects that I want to iterate over in a JSP page. I can do it with a scriptlet but I was hoping to use one of the taglibs. I've looked at <logic:iterate> but I'm not sure how to just pass it a List which is sitting in the request object. I really don't see the point in creating an object just to wrap around the List.... so am I missing something or is there a better way? :)


Thanks in advance! :)
0
Comment
Question by:petepalmer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 13630533
0
 
LVL 1

Author Comment

by:petepalmer
ID: 13630558
He appears to be trying the same thing I am.

What it appears to want is the name of a bean and then the method to call to get the list. However I haven't got a bean with such a getter - I've just got a List object in the request scope which afaik doesn't have a method to return itself  :)

0
 
LVL 37

Accepted Solution

by:
zzynx earned 400 total points
ID: 13630674
Another try:

At http://jakarta.apache.org/velocity/ymtd/ymtd-taglibs.html I found:

<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<%
java.util.ArrayList list = new java.util.ArrayList();
  list.add("First");
  list.add("Second");
  list.add("Third");
  list.add("Fourth");
  list.add("Fifth");
  pageContext.setAttribute("list", list, PageContext.PAGE_SCOPE);
%>

<logic:iterate id="myCollectionElement" name="list">
  Element Value: <bean:write name="myCollectionElement" /><br />
</logic:iterate>

Useful?
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 2

Expert Comment

by:arnon81
ID: 13633193
I suppose hibernate gives you a collection of object
so something like this maybe work for you

<logic:iterate id="user" name="userlist">
Firstname: <bean:write name="user" property="firstname" /><br /> --> equivalent to say user.getFirstname()
Lastname: <bean:write name="user" property="lastname" />  --> equivalent to say user.getLastname()
</logic:iterate>
0
 
LVL 2

Assisted Solution

by:arnon81
arnon81 earned 600 total points
ID: 13633208
addition to my last post:
userlist is a variable type List which you have put on the request object

and
<logic:iterate id="user" name="userlist">
Firstname: <bean:write name="user" property="firstname" /><br />
Lastname: <bean:write name="user" property="lastname" />

can also be

<logic:iterate id="aa" name="userlist">
Firstname: <bean:write name="aa" property="firstname" /><br />
Lastname: <bean:write name="aa" property="lastname" />
</logic:iterate>
0
 
LVL 1

Author Comment

by:petepalmer
ID: 13633775
Well I solved this one by using arnon81's solution - but I'd already figured it out before that with help frm the info that zzynx gave.....

So I'm splitting the points on this one :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13650013
Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question