You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Gerald Hanks <ge...@fvision.com> on 2001/07/15 07:54:06 UTC

Preloading ActionForm data from database (newbie question)

I started an app where I have a client login and jump to a main menu.
>From that menu the client can modify their account info.  I have a
ClientInfoForm bean, a ClientInfoAction and a clientinfo.jsp input form.
I have also created a ClientInfoBean which has all of the business logic
to retrieve the clients info from the database and save it back out when
finished.

I have the ClientInfoForm doing all of the appropriate validation stuff
and the ClientInfoAction communicates fine with the ClientInfoBean to
save all of the data back to the database.  My problem is how and when
to get the ClientInfoForm bean to communicate with the ClientInfoBean so
that when the clientinfo.jsp form loads, the fields are properly filled
in.  Is it appropriate to create a constructor for the ClientInfoForm so
that when it is created the communication with the ClientInfoBean
happens then or is there a more appropriate way?

Thank you in advance,

-gerald


Re: Preloading ActionForm data from database (newbie question)

Posted by Gerald Hanks <ge...@fvision.com>.
Levi,

Thank you.  I was actually trying to link to the jsp page directly.  I see what
you are saying about the action-mapping.  I guess all I really need to do is
add a simple action that loads the ClientInfoBean and then forwards on to the
clientinfo.jsp page.  As long as I give the ClientInfoForm bean the ability to
talk to the ClientInfoBean then this approach will work.  It all seems so
straight forward now.  I guess that's why it was a newbie question.

-gerald

Levi Cook wrote:

> Hi Gerald,
>
> One point I'm not clear on from your descriptions, is whether you're
> directly accessing your .jsp files URL, or if you're accessing them via an
> action-mapping URL.
>
> For example:
> http://localhost/yourapp/clientinfo.jsp versus
> http://localhost/yourapp/clientinfo.do
>
> With Struts, the second form is (*typically*) preferred--
>
> I generally recommend doing something like this:
>
> <struts-config>
> ...
>   <form-bean
>     name="clientInfoForm"
>     type="com.yourco.struts.ClientInfoForm"/>
> ...
>   <action
>     path="/editClientInfo"
>     type="com.yourco.struts.ReadClientInfoAction"
>     name="clientInfoForm"
>     scope="request"
>     validate="false">
>     <forward name="success" path="/WEB-INF/jsp/editClientInfo.jsp"/>
>   </action>
>   <action
>     path="/saveClientInfo"
>     type="com.yourco.struts.SaveClientInfoAction"
>     name="clientInfoForm"
>     scope="request"
>     validate="true"
>     input="/WEB-INF/jsp/editClientInfo.jsp">
>     <forward name="success" path="/mainMenu.do" redirect="true"/>
>   </action>
> ...
> ..
> .
>
> With these mappings, the link on your mainMenu.jsp should look *similar* to
> this:
> <html:link page="/editClientInfo.do">
>  <bean:message key="mainMenu.label.editClientInfo"/>
> </html:link>
>
> When this URL is invoked, the Struts ActionServlet will instantiate a
> ClientInfoForm for you, then forward control to the ReadClientInfoAction,
> passing a reference to the ClientInfoForm. Once inside the Action, you can
> retrieve the appropriate ClientInfoBean and push it onto the Form. I've used
> a couple different approaches for this last step.
>
> 1) Use a Mediator to populate the form with the bean data
> 2) Make the form responsible for populating itself w/ a reference to the
> bean
>
> The first is useful for avoiding direct coupling between your form and bean.
> Basically, you're introducing a third object that knows the interface of
> both the form, and the bean. For example:
>
> public class BeanFormMediator {
>   public void populateClientInfoForm(ClientInfoForm form, ClientInfoBean
> bean) {...}
> }
>
> The second approach, which I generally prefer, assigns responsibility to the
> form for knowing about the bean.
> Note: I usually make this method package private-- ideally, the action (same
> package) is the only object that knows about this implementation detail.
>
> public class ClientInfoForm {
>   void populate(ClientInfoBean cib) {...}
> }
>
> Of course, there are caveats with any approach, and there is always another
> way to skin a cat etc.. However, this approach has worked quite well for me
> on several projects.
>
> HTH,
> Levi
>
> ----- Original Message -----
> From: "Gerald Hanks" <ge...@fvision.com>
> To: <st...@jakarta.apache.org>
> Sent: Sunday, July 15, 2001 7:55 AM
> Subject: Re: Preloading ActionForm data from database (newbie question)
>
> > Klaus,
> >
> > Thank you for the response.  Let me see if I can explain a little bit more
> > about what I have done and see if this changes your answer at all.
> >
> > I have two jsp forms right now.  One is login.jsp and the other is
> > signup.jsp.  Each one has an associated Form and Action.  If the login
> > fails it goes back to the login.jsp page.  If the login succeeds, it loads
> > the mainmenu.jsp page.  If the signup fails it goes back to itself if it
> > succeeds it goes to the mainmenu.jsp page as well.
> >
> > Both the login and signup when successful load the client_id into a
> > ClientBean along with a timestamp so that I can track how long the client
> > has been active (and wether or not to invalidate the session because of
> > inactivity).  Since this application could potentially have thousands of
> > active users logged in I do not want to have a lot of data hanging around
> > in "session" beans.
> >
> > I would like to have the client_info data loaded from the database only
> > when the client needs to review or modify it.  I do not want to keep it
> > around for longer than the "request".  From the mainmenu.jsp the client
> > can choose a link that goes to the clientinfo.jsp form where they can
> > review their client info and make changes.  The clientinfo.jsp has been
> > linked with the ClientInfoForm bean and ClientInfoAction to handle the
> > validation and saving of the modified data.  My problem is that the
> > ClientInfoBean that holds the data to be modified is not loaded at the
> > time that the clientinfo.jsp is loaded.  Because of this, there are no
> > values loaded into the form.  The form is blank (as if the client_info did
> > not exist).  I need the ClientInfoForm bean to talk to the ClientInfoBean
> > before the jsp page starts loading the form (so that the current data can
> > be loaded into the form).  Does this help out at all?
> >
> > -gerald
> >
> > Klaus Thiele wrote:
> >
> > > hi gerald,
> > >
> > > i don't now if i really understand your problem.
> > > in my opinion, the ClientInfoForm has noting to do with your
> > > ClientInfoBean.
> > >
> > > i would prefere this (regards to the struts-sample-app):
> > >
> > > 1) logon.jsp - LogonAction - LogonForm
> > > LogonForm validates the fields username and password are filled out by
> > > the user.
> > > LogonAction instantiates your ClientInfoBean and call some methods on
> > > it to verify if the user is already registrated (read the record from
> > > the database) - if not, error and back to the logon-page (or create a
> > > new account,....).
> > > but if the user is registrated, put the ClientInfoBean-Object into the
> > > session-context  and go to the main-menu.
> > > 2) clientinfo.jsp - Edit/SaveClientInfoAction - ClientInfoForm
> > > EditClientInfoAction retrieve the ClientInfoBean-Object from the
> > > session-context, calls some methods on it to fill the fields in
> > > ClientInfoForm.
> > > if submit is clicked, do some simple validations in
> > > ClientInfoForm (are the fields filled out, date's valid entered,...).
> > > in SaveClientInfoAction retrieve the ClientInfoBean-Object from the
> > > session-context, call the validation-methods on your ClientInfoBean and
> > > save the data, display an error,....
> > >
> > > hope that helps
> > >   klaus
> > >
> > > Am Sonntag, 15. Juli 2001 05:54 schrieben Sie:
> > > > I started an app where I have a client login and jump to a main menu.
> > > > From that menu the client can modify their account info.  I have a
> > > > ClientInfoForm bean, a ClientInfoAction and a clientinfo.jsp input
> > > > form. I have also created a ClientInfoBean which has all of the
> > > > business logic to retrieve the clients info from the database and
> > > > save it back out when finished.
> > > >
> > > > I have the ClientInfoForm doing all of the appropriate validation
> > > > stuff and the ClientInfoAction communicates fine with the
> > > > ClientInfoBean to save all of the data back to the database.  My
> > > > problem is how and when to get the ClientInfoForm bean to communicate
> > > > with the ClientInfoBean so that when the clientinfo.jsp form loads,
> > > > the fields are properly filled in.  Is it appropriate to create a
> > > > constructor for the ClientInfoForm so that when it is created the
> > > > communication with the ClientInfoBean happens then or is there a more
> > > > appropriate way?
> > > >
> > > > Thank you in advance,
> > > >
> > > > -gerald
> > >
> > > --
> > > Klaus Thiele - Personal & Informatik AG
> > > mailto:kth@pi-ag.com
> > >
> > >  "Your mouse has moved.
> > >   Windows must be restarted for the change to take effect."
> >
> >


Re: Preloading ActionForm data from database (newbie question)

Posted by Levi Cook <le...@papajo.com>.
Hi Gerald,

One point I'm not clear on from your descriptions, is whether you're
directly accessing your .jsp files URL, or if you're accessing them via an
action-mapping URL.

For example:
http://localhost/yourapp/clientinfo.jsp versus
http://localhost/yourapp/clientinfo.do

With Struts, the second form is (*typically*) preferred--

I generally recommend doing something like this:

<struts-config>
...
  <form-bean
    name="clientInfoForm"
    type="com.yourco.struts.ClientInfoForm"/>
...
  <action
    path="/editClientInfo"
    type="com.yourco.struts.ReadClientInfoAction"
    name="clientInfoForm"
    scope="request"
    validate="false">
    <forward name="success" path="/WEB-INF/jsp/editClientInfo.jsp"/>
  </action>
  <action
    path="/saveClientInfo"
    type="com.yourco.struts.SaveClientInfoAction"
    name="clientInfoForm"
    scope="request"
    validate="true"
    input="/WEB-INF/jsp/editClientInfo.jsp">
    <forward name="success" path="/mainMenu.do" redirect="true"/>
  </action>
...
..
.

With these mappings, the link on your mainMenu.jsp should look *similar* to
this:
<html:link page="/editClientInfo.do">
 <bean:message key="mainMenu.label.editClientInfo"/>
</html:link>

When this URL is invoked, the Struts ActionServlet will instantiate a
ClientInfoForm for you, then forward control to the ReadClientInfoAction,
passing a reference to the ClientInfoForm. Once inside the Action, you can
retrieve the appropriate ClientInfoBean and push it onto the Form. I've used
a couple different approaches for this last step.

1) Use a Mediator to populate the form with the bean data
2) Make the form responsible for populating itself w/ a reference to the
bean

The first is useful for avoiding direct coupling between your form and bean.
Basically, you're introducing a third object that knows the interface of
both the form, and the bean. For example:

public class BeanFormMediator {
  public void populateClientInfoForm(ClientInfoForm form, ClientInfoBean
bean) {...}
}


The second approach, which I generally prefer, assigns responsibility to the
form for knowing about the bean.
Note: I usually make this method package private-- ideally, the action (same
package) is the only object that knows about this implementation detail.

public class ClientInfoForm {
  void populate(ClientInfoBean cib) {...}
}


Of course, there are caveats with any approach, and there is always another
way to skin a cat etc.. However, this approach has worked quite well for me
on several projects.

HTH,
Levi

----- Original Message -----
From: "Gerald Hanks" <ge...@fvision.com>
To: <st...@jakarta.apache.org>
Sent: Sunday, July 15, 2001 7:55 AM
Subject: Re: Preloading ActionForm data from database (newbie question)


> Klaus,
>
> Thank you for the response.  Let me see if I can explain a little bit more
> about what I have done and see if this changes your answer at all.
>
> I have two jsp forms right now.  One is login.jsp and the other is
> signup.jsp.  Each one has an associated Form and Action.  If the login
> fails it goes back to the login.jsp page.  If the login succeeds, it loads
> the mainmenu.jsp page.  If the signup fails it goes back to itself if it
> succeeds it goes to the mainmenu.jsp page as well.
>
> Both the login and signup when successful load the client_id into a
> ClientBean along with a timestamp so that I can track how long the client
> has been active (and wether or not to invalidate the session because of
> inactivity).  Since this application could potentially have thousands of
> active users logged in I do not want to have a lot of data hanging around
> in "session" beans.
>
> I would like to have the client_info data loaded from the database only
> when the client needs to review or modify it.  I do not want to keep it
> around for longer than the "request".  From the mainmenu.jsp the client
> can choose a link that goes to the clientinfo.jsp form where they can
> review their client info and make changes.  The clientinfo.jsp has been
> linked with the ClientInfoForm bean and ClientInfoAction to handle the
> validation and saving of the modified data.  My problem is that the
> ClientInfoBean that holds the data to be modified is not loaded at the
> time that the clientinfo.jsp is loaded.  Because of this, there are no
> values loaded into the form.  The form is blank (as if the client_info did
> not exist).  I need the ClientInfoForm bean to talk to the ClientInfoBean
> before the jsp page starts loading the form (so that the current data can
> be loaded into the form).  Does this help out at all?
>
> -gerald
>
> Klaus Thiele wrote:
>
> > hi gerald,
> >
> > i don't now if i really understand your problem.
> > in my opinion, the ClientInfoForm has noting to do with your
> > ClientInfoBean.
> >
> > i would prefere this (regards to the struts-sample-app):
> >
> > 1) logon.jsp - LogonAction - LogonForm
> > LogonForm validates the fields username and password are filled out by
> > the user.
> > LogonAction instantiates your ClientInfoBean and call some methods on
> > it to verify if the user is already registrated (read the record from
> > the database) - if not, error and back to the logon-page (or create a
> > new account,....).
> > but if the user is registrated, put the ClientInfoBean-Object into the
> > session-context  and go to the main-menu.
> > 2) clientinfo.jsp - Edit/SaveClientInfoAction - ClientInfoForm
> > EditClientInfoAction retrieve the ClientInfoBean-Object from the
> > session-context, calls some methods on it to fill the fields in
> > ClientInfoForm.
> > if submit is clicked, do some simple validations in
> > ClientInfoForm (are the fields filled out, date's valid entered,...).
> > in SaveClientInfoAction retrieve the ClientInfoBean-Object from the
> > session-context, call the validation-methods on your ClientInfoBean and
> > save the data, display an error,....
> >
> > hope that helps
> >   klaus
> >
> > Am Sonntag, 15. Juli 2001 05:54 schrieben Sie:
> > > I started an app where I have a client login and jump to a main menu.
> > > From that menu the client can modify their account info.  I have a
> > > ClientInfoForm bean, a ClientInfoAction and a clientinfo.jsp input
> > > form. I have also created a ClientInfoBean which has all of the
> > > business logic to retrieve the clients info from the database and
> > > save it back out when finished.
> > >
> > > I have the ClientInfoForm doing all of the appropriate validation
> > > stuff and the ClientInfoAction communicates fine with the
> > > ClientInfoBean to save all of the data back to the database.  My
> > > problem is how and when to get the ClientInfoForm bean to communicate
> > > with the ClientInfoBean so that when the clientinfo.jsp form loads,
> > > the fields are properly filled in.  Is it appropriate to create a
> > > constructor for the ClientInfoForm so that when it is created the
> > > communication with the ClientInfoBean happens then or is there a more
> > > appropriate way?
> > >
> > > Thank you in advance,
> > >
> > > -gerald
> >
> > --
> > Klaus Thiele - Personal & Informatik AG
> > mailto:kth@pi-ag.com
> >
> >  "Your mouse has moved.
> >   Windows must be restarted for the change to take effect."
>
>


Re: Preloading ActionForm data from database (newbie question)

Posted by Gerald Hanks <ge...@fvision.com>.
Klaus,

Thank you for the response.  Let me see if I can explain a little bit more
about what I have done and see if this changes your answer at all.

I have two jsp forms right now.  One is login.jsp and the other is
signup.jsp.  Each one has an associated Form and Action.  If the login
fails it goes back to the login.jsp page.  If the login succeeds, it loads
the mainmenu.jsp page.  If the signup fails it goes back to itself if it
succeeds it goes to the mainmenu.jsp page as well.

Both the login and signup when successful load the client_id into a
ClientBean along with a timestamp so that I can track how long the client
has been active (and wether or not to invalidate the session because of
inactivity).  Since this application could potentially have thousands of
active users logged in I do not want to have a lot of data hanging around
in "session" beans.

I would like to have the client_info data loaded from the database only
when the client needs to review or modify it.  I do not want to keep it
around for longer than the "request".  From the mainmenu.jsp the client
can choose a link that goes to the clientinfo.jsp form where they can
review their client info and make changes.  The clientinfo.jsp has been
linked with the ClientInfoForm bean and ClientInfoAction to handle the
validation and saving of the modified data.  My problem is that the
ClientInfoBean that holds the data to be modified is not loaded at the
time that the clientinfo.jsp is loaded.  Because of this, there are no
values loaded into the form.  The form is blank (as if the client_info did
not exist).  I need the ClientInfoForm bean to talk to the ClientInfoBean
before the jsp page starts loading the form (so that the current data can
be loaded into the form).  Does this help out at all?

-gerald

Klaus Thiele wrote:

> hi gerald,
>
> i don't now if i really understand your problem.
> in my opinion, the ClientInfoForm has noting to do with your
> ClientInfoBean.
>
> i would prefere this (regards to the struts-sample-app):
>
> 1) logon.jsp - LogonAction - LogonForm
> LogonForm validates the fields username and password are filled out by
> the user.
> LogonAction instantiates your ClientInfoBean and call some methods on
> it to verify if the user is already registrated (read the record from
> the database) - if not, error and back to the logon-page (or create a
> new account,....).
> but if the user is registrated, put the ClientInfoBean-Object into the
> session-context  and go to the main-menu.
> 2) clientinfo.jsp - Edit/SaveClientInfoAction - ClientInfoForm
> EditClientInfoAction retrieve the ClientInfoBean-Object from the
> session-context, calls some methods on it to fill the fields in
> ClientInfoForm.
> if submit is clicked, do some simple validations in
> ClientInfoForm (are the fields filled out, date's valid entered,...).
> in SaveClientInfoAction retrieve the ClientInfoBean-Object from the
> session-context, call the validation-methods on your ClientInfoBean and
> save the data, display an error,....
>
> hope that helps
>   klaus
>
> Am Sonntag, 15. Juli 2001 05:54 schrieben Sie:
> > I started an app where I have a client login and jump to a main menu.
> > From that menu the client can modify their account info.  I have a
> > ClientInfoForm bean, a ClientInfoAction and a clientinfo.jsp input
> > form. I have also created a ClientInfoBean which has all of the
> > business logic to retrieve the clients info from the database and
> > save it back out when finished.
> >
> > I have the ClientInfoForm doing all of the appropriate validation
> > stuff and the ClientInfoAction communicates fine with the
> > ClientInfoBean to save all of the data back to the database.  My
> > problem is how and when to get the ClientInfoForm bean to communicate
> > with the ClientInfoBean so that when the clientinfo.jsp form loads,
> > the fields are properly filled in.  Is it appropriate to create a
> > constructor for the ClientInfoForm so that when it is created the
> > communication with the ClientInfoBean happens then or is there a more
> > appropriate way?
> >
> > Thank you in advance,
> >
> > -gerald
>
> --
> Klaus Thiele - Personal & Informatik AG
> mailto:kth@pi-ag.com
>
>  "Your mouse has moved.
>   Windows must be restarted for the change to take effect."


Re: Preloading ActionForm data from database (newbie question)

Posted by Klaus Thiele <kt...@pi-ag.com>.
hi gerald,

i don't now if i really understand your problem.
in my opinion, the ClientInfoForm has noting to do with your 
ClientInfoBean.

i would prefere this (regards to the struts-sample-app):

1) logon.jsp - LogonAction - LogonForm
LogonForm validates the fields username and password are filled out by 
the user.
LogonAction instantiates your ClientInfoBean and call some methods on 
it to verify if the user is already registrated (read the record from 
the database) - if not, error and back to the logon-page (or create a 
new account,....).
but if the user is registrated, put the ClientInfoBean-Object into the 
session-context  and go to the main-menu.
2) clientinfo.jsp - Edit/SaveClientInfoAction - ClientInfoForm
EditClientInfoAction retrieve the ClientInfoBean-Object from the 
session-context, calls some methods on it to fill the fields in 
ClientInfoForm.
if submit is clicked, do some simple validations in 
ClientInfoForm (are the fields filled out, date's valid entered,...).
in SaveClientInfoAction retrieve the ClientInfoBean-Object from the 
session-context, call the validation-methods on your ClientInfoBean and 
save the data, display an error,....

hope that helps
  klaus

Am Sonntag, 15. Juli 2001 05:54 schrieben Sie:
> I started an app where I have a client login and jump to a main menu.
> From that menu the client can modify their account info.  I have a
> ClientInfoForm bean, a ClientInfoAction and a clientinfo.jsp input
> form. I have also created a ClientInfoBean which has all of the
> business logic to retrieve the clients info from the database and
> save it back out when finished.
>
> I have the ClientInfoForm doing all of the appropriate validation
> stuff and the ClientInfoAction communicates fine with the
> ClientInfoBean to save all of the data back to the database.  My
> problem is how and when to get the ClientInfoForm bean to communicate
> with the ClientInfoBean so that when the clientinfo.jsp form loads,
> the fields are properly filled in.  Is it appropriate to create a
> constructor for the ClientInfoForm so that when it is created the
> communication with the ClientInfoBean happens then or is there a more
> appropriate way?
>
> Thank you in advance,
>
> -gerald

--
Klaus Thiele - Personal & Informatik AG
mailto:kth@pi-ag.com

 "Your mouse has moved.
  Windows must be restarted for the change to take effect."