File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Iterating through a collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Iterating through a collection" Watch "Iterating through a collection" New topic
Author

Iterating through a collection

Matt Sall
Greenhorn

Joined: Jul 01, 2007
Posts: 19
I am fetching data form a database using this code:
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(sURL, sUsername, sPassword);
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM users");
RowSetDynaClass rsdc = new RowSetDynaClass(res);
rows = rsdc.getRows();
request.setAttribute("coll", rows);

In my jsp page I want to iterate through that collection and prints out every item in it, I've tried this:
<logic :present name="coll">
<logic:iterate id="contents" name="coll" >
<bean:write name="contents" /><br>
</logic:iterate>
</logic :present>

ant the stack trace says: JspException: No getter method for property value of bean contents

The problem is that I don't know how to do this, I know how to iterate through e.g. a hashmap using something like this:
Map content: <bean:write name="contents" property="key"/> <bean:write name="contents" property="value"/><br>

How do I do something similar with a collection? Please someone shed some light on this, thanks.

[ August 26, 2007: Message edited by: Mattias Sarling ]

[ August 26, 2007: Message edited by: Mattias Sarling ]

[ August 26, 2007: Message edited by: Mattias Sarling ]
[ August 26, 2007: Message edited by: Mattias Sarling ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The statement:

will not work because you can't just display a dynabean. You must display some property of the bean corresponding to a column name in the database. Example:


Merrill
Consultant, Sima Solutions
Matt Sall
Greenhorn

Joined: Jul 01, 2007
Posts: 19
Yes, I have tried using this:
<bean:write name="contents" property="user_id" />
but I get the same error message. What if wanted to print out every name/value pair in the collection and I didn't know the columns in the DB, I'm using this "SELECT * FROM users". How can I do that, e.g. in plain java code if there's no way of doing it with tags?

Thanks.
Matt Sall
Greenhorn

Joined: Jul 01, 2007
Posts: 19
Problem solved, thanks anyway!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Mattias,

I'm glad you solved your problem. That's good news.

Since these forums are searchable, it would be helpful if you would take a few minutes to describe what you did to solve the problem. This will be helpful to anyone who runs in to the same problem and searches the forum for a solution.
Matt Sall
Greenhorn

Joined: Jul 01, 2007
Posts: 19
I used this code to iterate through the collection and print them:

Iterator it = col.iterator();
DynaBean dyna = (DynaBean) it.next();
DynaProperty[] props = dyna.getDynaClass().getDynaProperties();

for (int i = 0; i < props.length; i++) {
String propName = props[i].getName();
String propType = props[i].getType().getName();
String propVal = (String) dyna.get(propName);
System.out.println("Dyna props: " + i + " : " + propName + " : " + propVal + "\n");



}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Iterating through a collection