Tony Dahbura wrote the following on 7/6/2005 11:09 PM:
So I have a setup action. How do I in this setup action setup my
ActionForm values so when I forward control to the form they are there
and present for display. This form is being used to allow updating data
and I want to prepopulate the fields for the user.
My form is an editUserForm that has an editUser action as well. Should
I in my editUser action call the setters of the editUserForm and set the
values? Then just call the jsp page that will reference these through
the editUserForm? I agree using reset is not proper given the
documentation-just not sure how I should do this...
The editUserForm is in request scope as well not in session scope....
Ok Tony, before Michael replies with the use of another framework
(joking sort of:), this is very easy to do with Struts and as a side
note, I really don't see why people have to constantly make Struts more
confusing than it needs to me. You can accomplish 99% of what you want
with this simple design (and then I'll address your issue directly).
For the sake of this discussion I'm going to assume you are using a
DispatchAction. In case you don't know, a DispatchAction just lets you
combine similar types of behaviors into one Controller... so rather than
have an UpdateUserAction, DeleteUserAction, etc.. you have one Action...
UserAction with methods in it update(..), delete(...), etc.
So typically here's what I do and it covers 'almost' all of my
scenarios. I find it easier to work with examples and your "User" is a
good example to work with. Lets say we want to CRUD (create, retrieve,
update, delete) for a User.
1) Step 1 Create a UserActionForm
For simplicity it has just two main properties...
String userName;
Integer userID;
Also though we are going to provide the dispatch (or action) as
a property to give it default value of "add"
String dispatch = "add"
This way if we go right to the page it'll have 'add' by default
2) Step 2 Have a BusinessObject back reprsenting your User. I like to
pass Business objects (not tied to Struts to my backend), so you'll have
in this case a simple:
"UserVO" (value object.. could just call it "User" but for this
discussion seeing VO helps you understand what it is)
String userName;
Integer userID;
//NOTE: it helps if the properties of the VO and the ActionForm have
the same name. Not a requirement but makes things easier which I'll show
3) Create a "UserDispatchAction"
This will have the following methods:
(all with signature public ActionForward methodName (ActionMapping
mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response)
setUpForEdit(..)
setUpForAdd(..)
add(...)
edit(...)
delete(...)
getUsers(...)
Before I get to the setUpForEdit() lets just handle the others...
In all cases you will be submitting either a form or a link but in
the struts-config file this will map to our UserAction where we also
include the name of our UserActionForm, so our UserActionForm is always
populated.
So our Add method in our Action...
//**ADD****
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
UserActionForm userForm = (UserActionForm)form;
UserVO user = new UserVO():
//copy our form properties into the VO
PropertyUtils.copyProperties( user, userForm );
ourBackendDelegate.addUser( user );
return mapping.findForward("to_form");
}
// UPDATE and DELETE...
Same as above except for use of...
ourBackendDelegate.deleteUser( user );
ourBackendDelegate.updateUser( user );
// THE SET UP FOR EDIT
Ok, this is the one you were asking about. Now you have to think about
how you would get here? Typically you'd get to an edit page by clicking
on a user record to say "Hey, I want to edit this guy's user
information" So imagine a case where we have a list of users and
'userID" for each in the list, they click on the link and they'll come
to this method which will get our user and then forward them to a page
to do the editing. The method will look like...
public ActionForward setUpForEdit(ActionMapping mapping, ActionForm
form, HttpServletRequest request, HttpServletResponse response) throws
Exception {
UserActionForm userForm = (UserActionForm)form;
//userID is set in this form when the user clicked on the link
//lets get our real business object of this user, based on ID..
UserVO user = ourBackendDelegate.addUser( userForm.getUserID() );
//copy the other way this time, from UserVO to our Form...
PropertyUtils.copyProperties( userForm, user );
//finally we are going to reuse or form for add and edit, so
//we will set up our dispatch/action parameter
userForm.setDispatch("edit");
return mapping.findForward("to_form");
}
//The get users - this will display our users on the page
public ActionForward getUsers(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
//lets say pass in companyID
Integer companyID = Integer.valueOf( request.getParameter("companyID"));
//get our list of users to display on page
List users = ourBackendDelegate.getUsers( companyID );
request.setAttribute("users", users );
return mapping.findForward("to_display");
}
4) sample config...
<action path="/userMaintenance" type="com.foobar.UserAction"
name="userActionForm"
scope="request"
validate="false"
parameter="dispatch">
<forward name="to_form" path="/userForm.jsp"/>
<forward name="to_display" path="/displayUsers.jsp"/>
</action>
5) Sample form
<html:form action="/userMaintenance">
Name: <html:text property="name"/><br/>
<html:hidden property="userID"/>
<html:hidden property="dispatch"/>
<html:submit value="${userForm.dispatch}"/>
<!-- above button logic can be done many ways, just for
simplicity i'm using the dispatch name-->
<html:form>
6) Sample page displaying users lets them edit and delete them
<c:forEach items='${users}' var='users'>
<c:url var="editUrl" scope="page" value="/userMaintenance.do">
<c:param name="dispatch" value="setUpForEdit"/>
<c:param name="userID" value="${user.userID}"/>
</c:url>
<c:url var="removeUrl" scope="page" value="/userMaintenance.do">
<c:param name="dispatch" value="delete"/>
<c:param name="userID" value="${user.userID}"/>
</c:url>
${user.name} <a href="${editUrl}">[ EDIT ]</a> <a
href="${removeUrl}">[ DELETE ]</a>
</c:forEach>
Basically 90% of applications are doing something similar to the above.
Presenting data to users and often letting them click on things to edit.
Personally, this concept works well for me. There are several tweeks I
have to do for more complex things, but this is the 'meat' of most
applications.
hth,
--
Rick
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]