You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Langdon Stevenson <ls...@objectpositive.com> on 2004/10/10 15:01:34 UTC
Iterating through nested sets of objects
Hi
I am trying to output the contents of a set of nested Objects in a JSP
page using the <nested> taglib.
I have three objects that are persisted using Hibernate:
1. Student - (contains a HashSet of Project called 'projects')
2. Project - (contains a HashSet of Url called 'urls')
3. Url - (contains a string)
The Hibernate persistance seems to require the use of HashSets for
sub-objects to work. I have tried using an ArrayList, but get Hibernate
errors.
I set a student bean in session scope in my action and then pass control
to the jsp. The jsp looks something like:
<nested:root name="adamh">
<nested:iterate id="element1" property="projects">
<tr>
<td>
Name:
</td>
<td>
<nested:write name="element1" property="name"/>
</td>
</tr>
<tr>
<td>
Query:
</td>
<td>
<nested:write name="element1" property="query"/>
</td>
</tr>
</nested:iterate>
</nested:root>
When I run the above jsp code, it works fine and prints the name and
query string of the project below the student details. However when I
add the second level of iteration like this:
<nested:root name="adamh">
<nested:iterate id="element1" property="projects">
<tr>
<td>
Name:
</td>
<td>
<nested:write name="element1" property="name"/>
</td>
</tr>
<tr>
<td>
Query:
</td>
<td>
<nested:write name="element1" property="query"/>
</td>
</tr>
<nested:iterate name="element2" property="urls">
<tr>
<td>
<nested:write name="element2" property="url"/>
</td>
</tr>
</nested:iterate>
</nested:iterate>
</nested:root>
I get the following error:
javax.servlet.ServletException: Cannot find bean element2 in any scope
This makes sense (sort of), but leaves me with little clue as to what I
should be doing instead. All of the examples that I can find of using
the <nested:iterate> tag say that I should use the form:
COMPANY: <html:text property="companyName"/>
<nested:iterate property="departments">
Department: <nested:text property="deptName"/>
<nested:iterate property="employees">
Employee: <nested:text property="empName"/>
E-mail: <nested:text property="email"/>
</nested:iterate>
</nested:iterate>
(this from the Struttin' with Struts tutorial -
http://www.reumann.net/struts/nested.do)
However my reading of the iterate taglib docs say that with a HashTable
I must use the id="something" property="object property" form. As
mentioned, that works fine for one level of nesting, but not multiple
levels.
Any suggestions as to how I should be modifying my JSP? Keeping in mind
that Hibernate appears to limit me to using a HashMap to store my nested
objects. I expect that there is a simple answer, but can't for the life
of me find it.
I am using Struts 1.1 with Tomcat 4.0.6 / 4.1 on Win2k
Full listing of relevent files below.
Any assistance greatly appreciated.
Regards,
Langdon
---------------------
-- Student.java --
package adamh;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
/**
* Created by IntelliJ IDEA.
* Student: langdons
* Date: 12/08/2004
* Time: 18:58:49
* To change this template use Options | File Templates.
*/
public class Student implements Serializable {
private String id;
private String parentId;
private String emailAddress;
private String password;
private String nickname;
private String yearOfBirth;
private String country;
private String state;
private String gender;
private java.util.Date firstContact;
private String referredById;
private String agreeToRules;
private User user;
private Role role;
private Set projects = new HashSet();
public String getId() {
return id;
}
public void setId(String string) {
id = string;
}
public String getParentId() {
return parentId;
}
public void setParentId(String string) {
parentId = string;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String string) {
emailAddress = string;
}
public String getPassword() {
return password;
}
public void setPassword(String string) {
password = string;
}
public String getNickname() {
return nickname;
}
public void setNickname(String string) {
nickname = string;
}
public String getyearOfBirth() {
return yearOfBirth;
}
public void setYearOfBirth(String string) {
yearOfBirth = string;
}
public String getCountry() {
return country;
}
public void setCountry(String string) {
country = string;
}
public String getState() {
return state;
}
public void setState(String string) {
state = string;
}
public String getGender() {
return gender;
}
public void setGender(String string) {
gender = string;
}
public Date getFirstContact() {
return firstContact;
}
public void setFirstContact(Date date) {
firstContact = date;
}
public String getReferredById() {
return referredById;
}
public void setReferredById(String string) {
referredById = string;
}
public String getAgreeToRules() {
return agreeToRules;
}
public void setAgreeToRules(String string) {
agreeToRules = string;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public Set getProjects() {
return this.projects;
}
public void setProjects(Set project) {
this.projects = project;
}
public void addProject(Project project) {
project.setStudent(this);
projects.add(project);
}
}
-- Project.java --
package adamh;
import java.util.Set;
import java.util.HashSet;
/**
* Created by IntelliJ IDEA.
* User: langdons
* Date: 7/10/2004
* Time: 20:39:14
* To change this template use Options | File Templates.
*/
public class Project {
private String id;
private String student_id;
private String name;
private String query;
private Student student;
private Set urls = new HashSet();
public String getId() {
return id;
}
public void setId(String string) {
id = string;
}
public String getStudent_id() {
return student_id;
}
public void setStudent_id(String string) {
student_id = string;
}
public String getName() {
return name;
}
public void setName (String string) {
name = string;
}
public String getQuery () {
return query;
}
public void setQuery (String string) {
query = string;
}
public void setStudent(Student student){
this.student = student;
}
public Student getStudent() {
return student;
}
public Set getUrls() {
return urls;
}
public void setUrls(Set set) {
this.urls = set;
}
public void addUrls(Url url){
url.getProjects().add(url);
urls.add(url);
}
}
-- Url.java --
package adamh;
import java.util.Set;
import java.util.HashSet;
/**
* Created by IntelliJ IDEA.
* User: langdons
* Date: 7/10/2004
* Time: 21:13:35
* To change this template use Options | File Templates.
*/
public class Url {
private String id;
private String url;
private Set projects = new HashSet();
public String getId() {
return id;
}
public void setId(String string) {
id = string;
}
public String getUrl() {
return url;
}
public void setUrl (String string) {
url = string;
}
public Set getProjects () {
return this.projects;
}
public void setProjects (Set set) {
this.projects = set;
}
}
-- home.jsp --
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-nested" prefix="nested"%>
<html>
<head>
<title><bean:message key="studentHomePage.pageTitle" /></title>
</head>
<body>
<h1><bean:message key="studentHomePage.pageHeader" /></h1>
<table>
<tr>
<td colspan=2>
<html:messages id="message" message="true">
<%= message %>
</html:messages>
</td>
</tr>
<tr>
<td>
<bean:message
key="studentRegistrationWizard.referredById" />:
</td>
<td>
<bean:write name="adamh" property="referredById" />
</td>
</tr>
<tr>
<td>
<bean:message
key="studentRegistrationWizard.emailAddress" />:
</td>
<td>
<bean:write name="adamh" property="emailAddress" />
</td>
</tr>
<tr>
<td>
<bean:message key="studentRegistrationWizard.password" />:
</td>
<td>
<bean:write name="adamh" property="password" />
</td>
</tr>
<tr>
<td>
<bean:message key="studentRegistrationWizard.nickname" />:
</td>
<td>
<bean:write name="adamh" property="nickname" />
</td>
</tr>
<tr>
<td>
<bean:message key="studentRegistrationWizard.yearOfBirth"/>
</td>
<td>
<bean:write name="adamh" property="yearOfBirth" />
</td>
</tr>
<tr>
<td>
<bean:message key="studentRegistrationWizard.country"/>
</td>
<td>
<bean:write name="adamh" property="country" />
</td>
</tr>
<tr>
<td>
<bean:message key="studentRegistrationWizard.state"/>
</td>
<td>
<bean:write name="adamh" property="state" />
</td>
</tr>
<tr>
<td>
<bean:message key="studentRegistrationWizard.gender"/>
</td>
<td>
<bean:write name="adamh" property="gender" />
</td>
</tr>
<tr>
<td>
Projects:
</td>
</tr>
<nested:root name="adamh">
<nested:iterate id="element1" property="projects">
<tr>
<td>
Name:
</td>
<td>
<nested:write name="element1" property="name"/>
</td>
</tr>
<tr>
<td>
Query:
</td>
<td>
<nested:write name="element1"
property="query"/>
</td>
</tr>
<nested:iterate name="element2" property="urls">
<tr>
<td>
<nested:write name="element2"
property="url"/>
</td>
</tr>
</nested:iterate>
<tr>
<td colspan="2">
<hr>
</td>
</tr>
</nested:iterate>
</nested:root>
</table>
</body>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org