Thanks Rick,

I'd like to clarify something here. 


>    UserActionForm userForm = (UserActionForm)form;
>    //userID is set in this form when the user
> clicked on the link

So, even though the jsp containing the list of users
and the links to edit each of them doesnot contain a
form tag, your userForm will still be created and
assigned a userID? I guess that is the case due to the
fact that the UserForm is defined for the
DispatchUserAction and has a request scope so the form
will always be created when the action is invoked.

James

--- Rick Reumann <[EMAIL PROTECTED]> wrote:

> I posted this in reply to Tony's post, but figured
> this stuff comes up 
> all the time and to those new to Struts, this below
> might help....
> 
> You can accomplish 99% of what you want with this
> simple design
> 
> 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"/>
> 
=== message truncated ===


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to