I tend not to mix my ActionForms and any "business" type classes at all.
I keep the forms lightweight, even "dumb" by having only string data (this
also supports redisplay properly) In the action I take the data out of the
form and give it to the business layer.  This is 2-way separation - Struts
doesn't know about my app specific logic and my app specific logic (and
data) does not know about Struts).  A class like Money and MoneyForm will
look and feel like a bit of duplication, but as design goes (balancing of
forces and all that) I tolerate the duplication for the benefit of the
separation.  This way as Struts changes you can "upgrade" without worrrying
about new behaviors or features in the forms, etc.  And if you ever (God
forbid) had to get away from Struts, it would be a easy split on your app
code.

Tim



                                                                                       
                                                           
                    BootedBear                                                         
                                                           
                    <bootedbear@boote                                                  
                                                           
                    dbear.com>                                                         
                                                           
                                                                                       
                                                           
                    08/12/2002 11:47                                                   
                                                           
                    AM                To: [EMAIL PROTECTED]               
                                                           
                    Please respond to cc:                                              
                                                           
                    "Struts Users                                                      
                                                           
                    Mailing List"                                                      
                                                           
                                                                                       
                                                           
                                             Subject:     How do you handle non-text 
data in Struts forms?                                        
                                                                                       
                                                           



Caterpillar: Confidential Green          Retain Until: 09/11/2002
                                         Retention Category:  G90 -
                                         Information and Reports




Hello all, Struts newbie here. I'm in the process of evaluating Struts for
use in a project and one of the criteria I'm checking out is how to deal
with non-text data to and from a form.

For my test case I coded a fairly simple Money class that represents a
currency value (stored internally as an integer value of pennies). This
class has a factory method that can construct a Money instance from a
parsed text field, and has a toString() method that returns the value
formatted as its textual equivalent.

So in my AmountForm (extending ActionForm) I have the following instance
variable, setter, and getter for a field with the key "amount":

   private Money amount = null;

   public void setAmount( String text ) {
     this.amount = Money.parseText( text );
   }

   public String getAmount() {
     return this.amount.toString();
   }

So far, so good.

But of course, my actions aren't really interested in the text format of
the field, but in the Money instance itself. So I changed the getter to:

   public Money getAmount() {
     return this.amount;
   }

Figuring that because Money has a toString() method, that all would be
well: my actions could obtain the Money instance, and Struts could obtain
the text format for displaying on the pages.

Well, that broke things. Strangely enough, the symptom of the above change
seems to cause the setter to not be called upon form submission (all seems
to go well with the getter itself -- curious).

I'm not sure why the symptoms of the failure are as such, but my question
boils down to this:

What patterns do you use to handle this type of non-textual data in your
Struts forms?

Do you create 2 getters: the bean-patterned setter to return the text
value, AND a getter to get the Money (or other non-text) class instance? As
in:

   public String getAmount() { return this.amount.toString(); }
   public Money getMoneyAmount() { return this.amount; }

This seems rather messy to me, and since this is probably a common
requirement (use of non-text data), I figured someone must have come up
with a better pattern.

Thanks for any input you all might have!

bear


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

Reply via email to