At line 215 changed 3 lines. |
# Add logic to WeblogAction to update User's Roles |
# Insert record into database to show 2 users: insert into weblog_user values ('mraible', 1); |
# add class="tallCell" to pretty up view of users |
The WeblogForm is request-scoped, so that's why you have to put all the attributes of user as hidden fields in the page. Other options include making the form session-scoped, as well as re-fetching the object in your Action before saving it. All of these approaches have issues: |
At line 217 added 32 lines. |
* Request-scoped: Everything has to be in the page as editable or hidden fields. If you leave a field out, it'll get set to null when you save the page. There may also be security implications with using hidden fields - so don't use this approach if you have sensitive data. |
* Session-scoped: You have to clean up the session after successfully saving the form. |
* Re-fetching before save: You have to figure out which fields have changed and merge the two objects together. |
|
The ''request-scoped'' method is used in this example because it's one of the easiest to understand. This particular "editing users in a weblog form" probably wouldn't be used in the real world (rather you'd have a <select multiple="multiple"%gt;). However, it does show you how to edit a many-to-many on both ends. |
|
In __UserAction.java__, there is logic to grab the usersRoles, fetch the Role objects from the database, and set them on the User object. You need to replicate this functionality in WeblogAction. Add the following just after the ''convertLists()'' call in __WeblogAction__.''save()'': |
|
[{Java2HtmlPlugin |
|
convertLists(weblog); |
|
// loop through and make sure all the roles are saved on User |
if (weblog.getUsers() != null) { |
for (int i=0; i < weblog.getUsers().size(); i++) { |
User user = (User) weblog.getUsers().get(i); |
String[] userRoles = request.getParameterValues("users[" + i + "].userRoles"); |
|
for (int j = 0; userRoles != null && j < userRoles.length; j++) { |
String roleName = userRoles[j]; |
user.addRole((Role)mgr.getObject(Role.class, roleName)); |
} |
} |
} |
}] |
|
Run __ant deploy db-load__ and navigate to the Weblog with an [id of 2|http://localhost:8080/appfuse/editWeblog.html?weblogId=2]. This weblog should have one user assigned to it - the ''mraible'' user. To add an additional user, run the following against your database: |
|
{{{insert into weblog_user values ('tomcat', 2);}}} |
|
If you're using < AppFuse 1.9, You may notice that the ''disabled'' fields are highlighted with a border when you click on those fields - even though they aren't editable. To fix this, [apply this patch|https://appfuse.dev.java.net/source/browse/appfuse/web/scripts/global.js?r1=1.12&r2=1.13]. Also, note the use of ''class="tallCell'' on the <th> that holds the ''Users:'' caption. This is used to put the caption at the top of the cell. After inserting the ''tomcat'' user and refreshing the page - your screen should resemble the image below. |
|