[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Struts, ArrayLists and <logic:iterate>
My result set contains 3 rows. And at some point I wrote a snippet of
code that logged the number of elements to the ArrayList that I was
returning in the request, and I saw the proper number (3) of elements.
I am using 2 ArrayLists, one for each query.
Let me add a bit of code to show where things go wrong....
I've got one action, UserAction which calls 2 methods, findUser and
getCourses
protected ArrayList getCourses(String username) {
....
return courses;
}
protected UserModel findUser(String username){
....
return um;
}
public ActionForward execute{
....
ArrayList returnClasses = null;
UserModel um = findUser(username);
request.setAttribute(mapping.getAttribute(), um);
returnClasses = getCourses(username);
request.setAttribute("classes", returnClasses);
}
and then in the view when I try to iterate over whats stored in the
request "classes" attribute, I get only 1 of the 3 elements expected.
My struts-config.xml entry is:
<action
path="/AdminUsers"
type="greene.UserAction"
name="userForm"
scope="request"
validate="true"
input="/pages/AdminUsers.jsp">
<forward name="AdminUsers" path="/pages/AdminUsers.jsp" />
</action>
and the spot on the .jsp where I iterate over the classes in the array
list looks like:
<logic:iterate id="coursework" name="classes" />
<tr>
<td width="22%"><b></b></td>
<td width="35%"><bean:write name="coursework"
property="shortname" /> </td>
<td width="43%"> </td>
</tr>
</logic:iterate>
OK, remember, this is my first struts app...So be kind :) My java
experience is about 4 weeks longer than my 4 weeks of struts experience!
So again, I would not be as puzzled if I got NOTHING back, because I
would realize that I messed it up good. But the fact that I still get
ONE element back, that's why I am really confused because I know I'm
close.
In looking at the snippets I put in, this particular chunk has only one
ArrayList set in the request, the first object is a UserModel, but I do
this a few times in my application with different other objects and the
results is the same, only 1 element in the ArrayList.
Also I see I set the request object different - but I have a different
page where my ArrayList of ALL users gets returned just fine using the
request.setAttribute("classes", returnClasses) method, but it's the only
returned item in the action.
Also another place where I might be going wrong is the above mentioned
userForm is only used for the returning user. I thought about maybe I
needed a courseForm too, but its my understanding you can only specify
one form...
Well hopefully I have given you enough information to either fully
confuse you AND me, or maybe you can pick apart the flaws in my noob
logic.
Thanks for the help so far...
Matt
-----Original Message-----
From: Matthew Phillips [mailto:matthew_a_phillips@yahoo.com]
Sent: Monday, July 21, 2003 2:50 PM
To: ajug-members@ajug.org
Subject: Re: Struts, ArrayLists and <logic:iterate>
It sounds like either your result set is only
returning 1 row or your ArrayList isn't being properly
filled with all of the rows. Are you using 1
ArrayList for both queries or one ArrayList for each
query?
Matthew Phillips
--- Matthew Thebert
<matthewthebert@greeneconsults.com> wrote:
> I have a Struts application that I am developing
> that is giving me a bit
> of a headache...I am fairly new to both Java and
> Struts - and my first
> post of any sort to the list.
>
> I am doing a section of the application that
> involves user
> administration. I have one action that works
> correctly that displays
> all the users that are registered. That is the only
> thing this part
> does is query the database and display all of the
> users.
>
> I have another action that searches for a single
> user via database query
> and displays the users profile on the page where it
> can be edited or
> changed, that works. But also I want to have a
> second query for that
> page, which displays any classes the user might be
> enrolled in. I have
> to do 2 result sets for a number of reasons so the
> solution isn't
> modifying my query.
>
> The problem I am having is that the first query with
> the user data shows
> up fine, but when I iterate through the arraylist
> for the 2nd query,
> only 1 row is displayed, when there should be more.
>
> Both queries are done in the Action, stored in an
> ArrayList and returned
> to the view using the request and displayed using
> the <logic:iterate>
> tags.
>
> If I got NO results returned, I'd figure that I was
> way off, but I am
> actually getting one row back. This is the part
> that is killing me is
> only being able to display one row (the first result
> put in the
> ArrayList). That and the fact that I know I can use
> this method
> correctly since I have one previous action that I
> can display all of the
> users correctly on a view using this same method
> with an arraylist and
> iterate tages.
>
> I can provide code snippets if needed. I appreciate
> the help. I have
> tried about everything I can think of this past week
> and am at wits end!
>
> Thanks in advance for the help!
>
> Matt Thebert
>
__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com