When I have a form to edit multiple entities at once, I use indexed
properties in a similar manner to this example. What I do not do is allow
the list that I'm indexing into to be initialized before binding takes
place, for exactly the reason you have pointed out. Instead, I always have a
TypeConverter in place for the entity type that I'm editing, and in my
iterator I place a hidden input that allows Stripes to get the entity from
the TypeConverter and insert it into the list. Something like this:
== JSP ==
s:form ...
c:forEach var=entity varStatus=loop items=${actionBean.entities}
s:hidden name=entities[${loop.index}] /
s:text name=entities[${loop.index}].foo /
...
/c:forEach
/s:form
== ActionBean ==
@ValidateNestedProperties({ ... })
private ListFoo entities; // plus getter setter
@DontBind
public Resolution view() {
entities = getContext().getFooDao().list();
return new ForwardResolution(...);
}
public Resolution update() {
if (entities != null) {
for (Foo entity : entities) {
if (entity != null) {
getContext().getFooDao().save(entity);
}
}
}
return new RedirectResolution(...);
}
In my ActionBean, I only initialize the list when I'm forwarding to the
form, not when handling the form submission. And just to be safe, I disable
binding altogether for the view() method. In my JSP, I include a hidden
input for each iteration in the loop so that Stripes will bind the correct
entity into the list on form submission.
-Ben
On Tue, Mar 10, 2009 at 11:02 AM, Seth Duda sethd...@gmail.com wrote:
On page 317 there is an example of adding role checkboxes to a list of
users. Next to each user, the JSP adds a User and Administrator checkbox
where you can select roles for the user. Then, at the bottom of the screen,
there's a submit button to update all of the user roles in the list.
*
UserListActionBean.java:*
public class UserListActionBean extends BaseActionBean {
private ListUser users = userDao.read();
private ListUser getUsers() {
return users;
}
public ListRole getRoles() {
return roleDao.read();
}
public Resolution save() {
for( User user : users ) {
userDao.save(user);
}
userDao.commit();
}
}
*
user_list.jsp:*
c:set var=index value=0/
s:form
displaytag:table name=${actionBean.users} id=user /
...
displaytag:column
c:forEach var=role items=${actionBean.roles}
s:checkbox name=users[${index}].roles value=${role}
checked=${user.roles}
/c:forEach
c:set var=index value=${index+1}/
/displaytag:column
...
/displaytag:table
s:submit name=save/
/s:form
Now, the issue I have with this is what happens if the user list changes
before the user presses submit? The indexes assigined in
users[${index}].roles might no longer refer to the correct user.
How would you go about actually building something like this?
The best solution I can think of would be to define a
MapString,ListRoles userRolesMap in the action bean. Then, in the
table I would set the checkbox name to: userRolesMap['${user.id}']
Finally, to update the roles when the user presses save, loop through the
users, and check to see if the userRolesMap contains the user's id - if it
does assign the user roles and update the user.
Does anyone else have any better solutions to solve this?
Thanks!
--
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com___
Stripes-users mailing list
Stripes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/stripes-users