You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jeff Skubick <je...@mci.com> on 2004/02/02 18:05:58 UTC

indexed AND nested formbeans -- HOW?

indexed nested formbeans -- HOW?

Suppose I have a form that represents rows of users, each of whom is
identified by a unique integer id. Each row has two checkboxes, representing
"selected foo" and "selected bar" conditions, respectively.

I've gotten the impression that it's possible to use nested properties to
refer to the id and checkbox state for each user as:

changed_user.id, changed_user.foo, and changed_user.bar (.id comes from a
hidden formvar, .foo and .bar are null if their respective checkboxes are
unchecked, or hold their respective checkboxes' checked values if checked).
Let's call the bean to handle this "ChangedUserBean"

... and hold the collection of ChangedUserBean objects in the ActionForm as
an ArrayList of ChangedUserBean objects.

>From reading the docs for NestX, it looks like ChangedUserBean should have
the following methods:

public void setUserId(String value);
public String getUserId();
public void setFoo(String value);
public String getFoo();
public void setBar(String value);
public String getBar();

The big question now is, how do I set up the ActionForm associated with the
action triggered by the form submittal, and the jsp page defining the form
itself, so it will create a List/array of ChangedUserBean objects from the
form data? Along the same lines, how does Struts even KNOW that it's
supposed to create an object of type ChangedUserBean for each row? Or even
what constitutes a "row" for index-generation purposes?

I originally tried something in the ActionForm along the lines of...

private List changedUsers;
public void setChangedUser(List changedUserList) {
 changedUsers = changedUserList;
}
public ChangedUserBean[] getChangedUser() {
 if (changedUsers == null)
  return null;
 return (ChangedUserBean[])changedUsers.toArray(new
ChangedUserBean[changedUsers.size()]);
}

and set up the jsp page with the form something like...
<!-- rendered inside an iteration, one block per user/"row" -->
 <nested:nest property="changedUsers">
  <nested:hidden property="userId" value="<%id%>"/>
  <nested:checkbox property="foo"/>
  <nested:checkbox property="bar"/>
 </nested:nest>
<!-- end of logical "row" -->

... but THAT particular experiment ended with a complaint that
getChangedUsers returned a null value.

Intuitively, it would seem that there needs to be some way of indicating to
the jsp tags that the rendered names need to have unique generated names in
the final output html, like...
<input type="hidden" name="changedUser.userId.1" value="99">
<input type="checkbox" name="changedUser.foo.1" value="foo">
<input type="checkbox" name="changedUser.bar.1" value="bar">
...
<input type="hidden" name="changedUser.userId.20" value="2470">
<input type="checkbox" name="changedUser.foo.20" value="foo">
<input type="checkbox" name="changedUser.bar.20" value="bar">

Then, indicate to the form parsing subsystem that when parsing the submitted
form data, for each Set of formvars whose names begin with "changedUser."
and end with the same dotted numeric value (".1", ".2", ... , ".20"), it
needs to create a ChangedUserBean object and call the set method identified
by the middle part of the formvar name ("userId", "foo", and "bar"), then
call the ActionForm's setChangedUser(Object) method with the newly-created
ChangedUserBean so it can be retrieved a few moments later.

Finally, I could fetch the List<ChangedUserBean>/ChangedUserBean[] from the
ActionForm and iterate through it to fetch the values associated with each
user.

Assuming I'm not totally off and fantasizing about something that just can't
be done with Struts, how would I actually go about doing it? All the
documentation and examples I've found so far involve EITHER nested OR
indexed properties, but nothing that seems to use both at once.... the only
way I can even *fathom* for the component parsing the submitted form data to
figure out that it's supposed to create ChangedUserBean objects for each row
is to use introspection to look at the return type of getChangedUsers(),
notice it's an array of ChangedUserBean objects, and conclude that I need to
somehow create them from the submitted form data... but the only returntype
that doesn't seem to cause Struts to crash and burn before rendering the jsp
page or when processing the submitted form data is Object[] (which
effectively takes away any clue at all as to what kind of objects
setChangedUsers is expecting a list of).

Help.



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org