Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Iterating a List with Struts

Posted on 2005-03-25
7
Medium Priority
?
1,209 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
A solution for Fortify Path Manipulation.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month12 days, 12 hours left to enroll

578 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