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 List<User> users = userDao.read();
    ....
    private List<User> getUsers() {
        return users;
    }
    ....
    public List<Role> 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
"Map<String,List<Roles>> 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!
------------------------------------------------------------------------------
_______________________________________________
Stripes-users mailing list
Stripes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/stripes-users

Reply via email to