Hi. I'm fairly new to Struts, but I've gone through the Struts documentation
and KeyboardMonkey's helpful tutorial. What I want to do is (almost) right
out of the tutorial, but it's not working the way I would expect.
The list archives show that other people have had this problem, and the few
real solutions I've found seem to indicate that I'm doing the right thing,
but I keep getting exceptions. My understanding is complicated by the fact
that the tutorial shows how to nest lists within a form, but I'm not using a
form (surely that's not necessary?).
One clarification I need: the KM tutorial indicates that only Object[] can
be used for nested lists, even though the Struts taglib docs say that
logic:iterate can iterate over Collection, Enumerations, arrays, etc. Is
that still true? Or can I nest lists where the bean property getters return
java.util.Collection?
So let me describe what I have, and what I'm trying to do. BTW, simply using
a single-level <logic:iterate> tag works great. I'm using Tomcat 4.0.4b2
and the nightly build of Struts from a couple days ago.
I have a set of Locations that are in a City. I have a list of Cities. I
would like to list all the Cities, and under each City list all the
locations. My code manages to list all the Cities, but when I add tags &
HTML to list the Locations, I get variously tag nesting exceptions or null
pointer exceptions (and occasionally a "getter not found" exception),
depending on various ways of writing the code.
I have a City bean (called a "Site" for historical reasons, it will
eventually be changed) defined like this:
public class Site {
public String getCity() { return mCity; }
public void setCity(String inCity) { mCity = inCity; }
public String getState() { return mState; }
public void setState(String inState) { mState = inState; }
public Collection getLocations() { return mLocations; }
public void setLocations(Collection inLocations) { mLocations
= inLocations; }
private String mCity = null;
private String mState = null;
private Collection mLocations = null; // Vector of Locations
}
and a FindResults bean defined like this:
public class FindResults {
public Collection getCities() { return mCities; }
public void setCities(Collection inCities) { mCities =
inCities; }
private Collection mCities; // A Vector of Sites
}
There's also a simple Location bean:
public Location {
public String getName() { return mName; }
public void setName(String inName) { mName = inName; }
}
As a result of submitting a form, my FindAction object gets executed. It
takes the form input, does a JDBC lookup, then creates a FindResults bean
and puts it in the session state. The following (simplified) JSP displays
the list of cities satisfactorily:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ 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" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<jsp:useBean id="findResults" scope="session"
type="com.latencyzero.cc.FindResults"/>
<head>
<title>Search Results</title>
</head>
<body bgcolor="#ffffff">
<logic:iterate id="foundCity" name="findResults" property="cities">
<table>
<tr><td>
Locations in <bean:write name="foundCity" property="city"/>
</td></tr>
<tr><td>
<!-- insert location list here -->
</td></tr>
</table>
</logic:iterate>
</body>
</html:html>
It's when I try to nest a list that I run into trouble. I started by
replacing the "insert location list here" comment with the following:
<logic:iterate id="foundLocation" name="foundCity" property="locations">
<p><bean:write name="foundLocation" property="name"></p>
</logic:iterate>
As soon as I do that, I get "org.apache.jasper.compiler.ParseException: End
of content reached while more parsing required: tag nesting error?".
Various permutations of using "nested" instead of "logic" and "bean" result
in other errors. For example, if I use "nested:iterate" for the inner loop
(and on the bean write), I get null pointer exception (even if I remove the
'name="foundCity"' attribute).
I even tried to make a single outer loop (the very first example above) that
used a <nested:write> tag, just to see if I could get *something* "nested"
going. Using both "logic:iterate" and "nested:iterate", this resulted in a
null pointer exception.
Clearly, I'm missing some fundamental point about nesting. Some people's
responses on the list indicate that they've had success simply nesting
<logic:iterate> tags without trouble
(http://www.mail-archive.com/[email protected]/msg23493.html).
I'd be happy with that for this situation (I don't think I need the
implicit-dotting of hierarchies of properties for this simple operation).
TIA for any help!
BTW, is my approach of creating a FindResults session bean and then calling
<jsp:useBean> appropriate? Or is there a preferred way to accomplish this?
--
Rick
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>