This is my understanding of the roles Of ActionForm & Action classes & Business logic 
& the best
way to use them in a typical business system. It's in as plain English as I can manage.
Quite a lot of experimentation has gone into all this but I'm way less experienced at 
struts than
many on the list. I hope this helps the students & beginners.....

Actionform (also called form bean)
---------------------------------
Designed to simplify the transferring data to & from html forms. Also pre-validation 
can be
defined (in validate method) but I avoid this (validate="false" in struts-config). One 
set of
validation code in the business logic & none anywhere else is simplest (I hate 
duplicated code
with a passion!).

Action
------

Action logic if inital display being done
1 - fetch the data from DB (tiny system), from business class (medium sized system), 
or some sort
of facade class that fetches it from the business class (mega-monster super 
architected EJB
system)
2 - move data to form bean & forward to the jsp (struts form tags will get their data 
from the
form bean).
3 - fetch other data required by the jsp & place it in the request (such as lists of 
states for a
select box). An alternative is custom tags (or scriplets) on the jsp that fetch & 
display things
like this. I prefer to do it the 1st way & never any other as this is all fiddly 
anyway - the
fewer design patterns used the better.
4 - If you have data in the session still don't let the jsp get it from there, you may 
move it
elsewhere one day. It's a simple pattern to know your jsps only display what the 
Action class
presents to them. (I could be argued out of this!).

Action logic if validation being done
- get data from the form bean & shove it at the business class that validates it. the 
form bean
only has String versions of the data as this is what HTML uses. So your validation 
logic must
accept String input. 
- possibly re-set the value in the form bean from the validation class (it may have 
standardised
it's format or cleaned it up ie. user types +123.4 you re-display it as 123.40). I 
think the
business class should return the displayable format of numeric data too.

if form data valid
- tell the business class to save it & go to success action.
else
- move validation errors (in Errors object) to the request & forward to our form, so 
user can
correct errors.

My design pattern for Business Logic Validation
-----------------------------------------------
These handle String input, even for numeric data (method overloading yay!). I have 
methods name
setV.... (set & validate) which return a String error message or null if the the data 
is valid.
The string is the second 1/2 of the struts message key. The action appends it's name 
to the front
to get the full key.

   public String setVTotalPriceString(String totalPriceString) {
      if (totalPriceString not numeric..) {
         return "total.price.must.be.currency";
      }
   .
   return null;   //yay it's valid
   }

   public String SetVTotalPrice(Currency totalPrice) {
      if (value <= 0) return "value.must.be.greater.than.zero"
      .
      return null;
   {
public Currency getTotalPrice() {
   return totalPrice;
}
/**
* returns display format version. Note if last setV..String call was passed an invalid
* string that value is returned. 
*/ 
public String getTotalPriceString() {
   return totalPriceString;
}

==================================================================

please shout very loud if i'm wrong (or incomprehensible)!
Keith.



--- Jim Tomlinson <[EMAIL PROTECTED]> wrote:
> 
> We've had to deal with an analogous problem, though, which I think is
> applicable in a lot of situations. The form is being used to change values
> in a persistent store, but when first displaying the form we want to display
> those values currently in the store. We've gone the route of having the
> session-scope ActionForm's constructor pull the current values from the
> store (obviously via a business logic class, rather than in the form
> itself). Then when the form is posted, the Action can take the new values
> from the ActionForm and persist them (again using another class). Is this
> what most people do, or is there a better way?
> -- 
> Jim Tomlinson
> 
> > -----Original Message-----
> > From: Keith Bacon [mailto:[EMAIL PROTECTED]]
> > Sent: Thursday, January 10, 2002 7:11 AM
> > To: Struts Users Mailing List
> > Subject: RE: Populating a Form with values
> > 
> > 
> > I think you've missed the point of form beans.
> > On submission of an html form struts looks at the request 
> > parameters. If it can find a matching
> > set method in the form bean it calls it. ie. the form parms 
> > are copied into the form bean.
> > 
> > When you forward to a jsp with a form struts calls the get 
> > methods of the form bean to populate
> > the the matching form fields (that have been defined with 
> > struts tags).
> > 
> > You action class can deal with the form variables by calling 
> > the same get/set methods of the form
> > bean. This saves doing qqq = request.getParameter("qqq"); & 
> > testing for nulls etc.
> > 
> > That's the whole point of form beans - to manage form fields.
> > 
> > To get readonly data onto your jsp your action class should 
> > place it in the request where the jsp
> > can get at it easily.
> > 
> > Keith.
> > 
> > 
> > 
> > 
> > 
> > --- "Witt, Mike (OH35)" <[EMAIL PROTECTED]> wrote:
> > > I'm new to struts also, but the way I have been doing it is 
> > to set up an
> > > action and a form bean in the struts config.  The action 
> > code (or some other
> > > bean that the action code uses) for getting the data from 
> > the database and
> > > populating the form using something like
> > > (MyForm)form.setField(theFieldData);
> > > 
> > > Hope this helps.
> > > 
> > > Mike
> > > 
> > > -----Original Message-----
> > > From: Daniel Jaffa [mailto:[EMAIL PROTECTED]]
> > > Sent: Thursday, January 10, 2002 9:25 AM
> > > To: [EMAIL PROTECTED]
> > > Subject: Populating a Form with values
> > > 
> > > 
> > > I am new to struts so be nice (But i have worked with jsp 
> > for 2 years).  I 
> > > have a form that i need to populate a form with values from 
> > the database.  I
> > > 
> > > pass the form a data_base id to the form but i now need the 
> > form to populate
> > > 
> > > the formbean.  I know how to do this in jsp but i want to 
> > do the in the 
> > > struts way.
> > > 
> > > _________________________________________________________________
> > > MSN Photos is the easiest way to share and print your photos: 
> > > http://photos.msn.com/support/worldwide.aspx
> > > 
> > > 
> > > --
> > > To unsubscribe, e-mail:
> > > <mailto:[EMAIL PROTECTED]>
> > > For additional commands, e-mail:
> > > <mailto:[EMAIL PROTECTED]>
> > > 
> > > --
> > > To unsubscribe, e-mail:   
> <mailto:[EMAIL PROTECTED]>
> > For additional commands, e-mail:
> <mailto:[EMAIL PROTECTED]>
> > 
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Send FREE video emails in Yahoo! Mail!
> http://promo.yahoo.com/videomail/
> 
> --
> To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
> 


__________________________________________________
Do You Yahoo!?
Send FREE video emails in Yahoo! Mail!
http://promo.yahoo.com/videomail/

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

Reply via email to