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 Jsp and database using the logic:iterate tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Jsp and database using the logic:iterate tag" Watch "Jsp and database using the logic:iterate tag" New topic
Author

Jsp and database using the logic:iterate tag

MaryT Tsele
Greenhorn

Joined: Oct 30, 2006
Posts: 8
Hi guys.
I am a new programmer still learning java.Is it possible to use the logic:iterate tag twice in the same JSP.I have used it to retrieve data from the database as follows.

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

<html:form action="/User.do?action=add">
<h2>List of users</h2>
<table width="40%" border="3" >
<tr>
<th>UserId</th>
<th>Username</th>
<th>Password</th>
<th>Action</th>
<th>Action</th>
</tr>
<logic:iterate id="elem" name="results" type="za.co.johnboy.vo.UserVO">
<tr>
<td align="right"><bean:write name="elem" property="userId"/></td>
<td><bean:write name="elem" property="username"/></td>
<td align="right"><bean:write name="elem" property="password"/><br></td>
<td><a href="User.do?action=update&userId=<bean:write name='elem' property='userId'/>">[Update]</a></td>
<td><a href="User.do?action=delete&userId=<bean:write name='elem' property='userId'/>">[Delete]</a></td>
</tr>
</logic:iterate>
<tr>
<td colspan="6" align="center">
<html:submit value="Add User"/>
</td>
</tr>
</table>
</html:form>
Now I need to iterate through a different class in order to get the output on the same page. Please help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

What exactly is "results"?

Some constructs are suitable for multiple iterations, and some are not.
[ November 06, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
MaryT Tsele
Greenhorn

Joined: Oct 30, 2006
Posts: 8
Hi Bear.

Thank you for attending to my question.And please accept my appology for not using the forum.

The �results� is a collection of data that I am getting out of the database using the ff method from a class called UserDAO. So I have another class which is called RoleDAO which retrieves the roles for the users from the database in the same way as the UserDAO class. I need to put the output of both classes(results and roleResults) on the same jsp page.

[code:]
public class UserDAO {

public Collection getAllUsers(){

Session session = SessionUtil.currentSession();
Criteria criteria = session.createCriteria(User.class);
criteria.addOrder(Order.asc("username"));
Collection result = criteria.list();
return result;
}
}
[code:]

Class for Roles:

[code:]
public class RoleDAO {

public Collection getRoles(){

Session session = SessionUtil.currentSession();
System.out.println("Staring the session");
Criteria criteria = session.createCriteria(Role.class);
criteria.addOrder(Order.asc("roleName"));
Collection roleResult = criteria.list();
return roleResult;
}
}
[code:]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

I see that you are trying to use the code tags, but not correctly. Please read this for help on that.

Whatever structure you are passng to the page for iteration implements the Collection interface, but we don't know much else about it.

Constructs such as Lists and Maps are multiply iterable, while Enumerations and Iterators are not.

You could stick a line of code such as



and that would tell you the class type of the collection for further investigation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jsp and database using the logic:iterate tag