You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Tony Dahbura <to...@dahbura.com> on 2005/07/07 05:09:16 UTC

prepopulating the form again

Ok:
I have seen much dialog on this topic I originally asked.  I read the 
docs...

"This method is *not* the appropriate place to initialize form value
> for an "update" type page (this should be done in a setup Action). You
> mainly need to worry about setting checkbox values to false; most of
> the time you can leave this method unimplemented."
> 
> You say the ActionForm is "just a class" with "a simple lifecycle".
> That "lifecycle" is more than just a sequence of method invocations,
> but also the expected behavior of each of those invocations.  The
> documentation ("lifecycle specification") clearly defines that the
> only behavior that is expected during the invocation of the reset()
> method is to set checkbox values to false.

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....

Thanks,
Tony




Re: prepopulating the form again

Posted by Michael Jouravlev <jm...@gmail.com>.
On 7/7/05, Rick Reumann <st...@reumann.net> wrote:
> 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

[skipped]

No, I did not have intention to reply, because I already did
yesterday, and also because Tony clealry stated that he wants (for
whatever reasons, probaly religions ones, but we are being tolerable
here, aren't we?) to use request scope instead of session scope for
his form beans, and also because he wants to go "by the book". My
stuff works with session scope only by design, and it is not "in the
book" yet.

> and as a side note, I really don't see why people have to constantly
> make Struts more confusing than it needs to me.

Hopefully, this side note does not relate to my library. If it does,
you probably have not tried it yet ;-) or you do not see any added
value in the goals that it solves.

Michael.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: prepopulating the form again

Posted by Rick Reumann <st...@reumann.net>.
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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org