You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jim Clayson <ji...@Infogain.co.uk> on 2002/07/04 18:58:03 UTC

should I use form bean and javabean for the same data?

Hi,
 
I read somewhere on this list sometime ago that one should bear in mind that
struts form beans should be considered to be part of the view in your
run-of-the-mill MVC struts app and that they should only be used for
prepopulating a form and/or reporting surface level validation errors.
 
Does this mean that I shouldn't ever be setting the values on the form bean
inside my action class or wherever I perform my business logic? If this is
true (and please could someone confirm this), should I be using a vanilla
javabean to model the data for use outside of the view?
 
Maybe I'm missing something here.
 
Any assistance will be much appreciated.
 
Jim 

__________________________________________________________ 

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Jim Clayson


Infogain Limited


tel: 01628 580600
fax: 01628 580610
email: jimc@infogain.co.uk <ma...@infogain.co.uk>  
web: www.infogain.com <http://www.infogain.com/>  

 

Disclaimer:  Neither this e-mail nor any attachment places any legal or
contractual obligations on Infogain Limited. Any reproduction, disclosure or
dissemination beyond the intended addressees is strictly prohibited save for
the legitimate business purposes of Infogain Limited and its clients or
partners.

__________________________________________________________ 

 
 

Re: should I use form bean and javabean for the same data?

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Thu, 4 Jul 2002, Jim Clayson wrote:

> Date: Thu, 4 Jul 2002 17:58:03 +0100
> From: Jim Clayson <ji...@Infogain.co.uk>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: "'struts-user@jakarta.apache.org'" <st...@jakarta.apache.org>
> Subject: should I use form bean and javabean for the same data?
>
> Hi,
>
> I read somewhere on this list sometime ago that one should bear in mind that
> struts form beans should be considered to be part of the view in your
> run-of-the-mill MVC struts app and that they should only be used for
> prepopulating a form and/or reporting surface level validation errors.
>
> Does this mean that I shouldn't ever be setting the values on the form bean
> inside my action class or wherever I perform my business logic? If this is
> true (and please could someone confirm this), should I be using a vanilla
> javabean to model the data for use outside of the view?
>

It's hard to be very descriptive without a particular example, but in
general I would say that model-tier data is generally exposed to the view
tier as JavaBeans.  If you are using some of the popular design patterns
for multi-tier applications (such as those in "Core J2EE Patterns" by
Alur/Crupi/Malks), this object will typically be a Data Access Object, or
a Value Object, or (a more current term) Data Transfer Object.

It's ok to expose model data directly to the view tier for display-only
use, by saving it in a request or session attribute where the view page
can get to it.  For the incoming data that modifies things, I generally
set up my applications like this:

* "Setup" action creates and prepopulates the form bean, as well
  as setting up any other beans that are needed for the display.
  It then forwards to the "input form" page.

* The "input form" page as an <html:form> tag pointing at the
  "Process" action, with input fields that match the properties
  of the corresponding form bean.  In most cases, the properties
  of the form bean will be strings.

* The form bean has a validate() method to check for things like
  required fields and conversion errors (such as entering "1a3"
  into a field destined to be an integer).  If any errors are found,
  Struts redisplays the "input form" page for you, with the exact
  values that the user entered (even if they were wrong).  Note that
  we have *not* corrupted any model tier objects with invalid data.

* The "Process" action is called only after validation is successful.
  It can perform any additional checks it needs, and then interacts
  with the business objects to perform whatever updates are needed.
  (In trivially simple Struts apps, the business logic is sometimes
  embedded in the Action -- it's better to segregate this logic into
  separate business objects, though).

* At this point, the form bean gets thrown away, because it has
  completed its purpose.  For request-scope form beans, this happens
  automatically when the request completes.  For session-scope form
  beans, you have to deliberately remove the coresponding session
  attribute.

> Maybe I'm missing something here.
>
> Any assistance will be much appreciated.
>
> Jim
>

Craig


> __________________________________________________________
>
> <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
>
> Jim Clayson
>
>
> Infogain Limited
>
>
> tel: 01628 580600
> fax: 01628 580610
> email: jimc@infogain.co.uk <ma...@infogain.co.uk>
> web: www.infogain.com <http://www.infogain.com/>
>
>
>
> Disclaimer:  Neither this e-mail nor any attachment places any legal or
> contractual obligations on Infogain Limited. Any reproduction, disclosure or
> dissemination beyond the intended addressees is strictly prohibited save for
> the legitimate business purposes of Infogain Limited and its clients or
> partners.
>
> __________________________________________________________
>
>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>