You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ted Husted <hu...@apache.org> on 2002/03/03 15:18:25 UTC

Re: Best way to deal with Form object String parameters into another bean?

Rick Reumann wrote:
> Before looking into this struts framework, my controler servlet would
> pass the request object off to the appropriate Action class which in
> turn might hand the request object to another class method that would
> create an EmployeeBean from the request object parameters. Than after
> that EmployeeBean was returned the appropriate method in the business
> logic tier was called such as businessTierObject.addEmployee(
> employeeBean );

Do that, except you can create the EmployeeBean from the EmployeeForm
instead. 

If all the public properties you want to transfer are native types, and
everything has been validated so you know it should convert, you can
just do something like:

 BeanUtils.populate(employeeBean,BeanUtils.describe(employeeForm));

Or, you can do it the hard way, and just call getters inside of setters. 

employeeBean.setProperty(new Integer(employeeForm.getProperty()));

If the properties don't match up exactly, you can used adapter methods
that do whatever transformation is needed. For example, the form may put
formatting characters in the property displayed to the user, but you may
need to strip those out for the database. 

    public void setTelephoneText(String telephone) {
        setTelephone(ConvertUtils.getDigits(telephone));
    }


The databse would use setTelephone() and getTelephone(), a string of
digits, but the form would use setTelephoneText and getTelephoneText,
which format the string for display. The same can go for a property that
needed to be localized, or a non-native type, like Date or Timestamp.

If anyone wants to do more research, and summarize for the group, there
are some starter links on the Newbie FAQ page (under construction,
contributions welcome). 

http://jakarta.apache.org/struts/newbie.html

Something to keep in mind is that there will not be a single best
practice. The ~best~ way to do a lot of these things depends on how the
model is set up. Since Struts is model-neutral, the model can be set up
anyway at all, and the part were the HTTP form data is transferred to
the Model data bean can also be set up anyway at all. Sometimes the
model may be set up to handle conversions from Strings (as ResultSets
and RowSets are), and sometimes you need to do this yourself.

-- Ted Husted, Husted dot Com, Fairport NY US
-- Developing Java Web Applications with Struts
-- Tel: +1 585 737-3463
-- Web: http://husted.com/about/services


Rick Reumann wrote:
> 
> I've just begun studying the struts examples and reading the
> documentation and have waited a bit before I posted this question. I
> am a newbie to struts but have done some searching and looking at
> examples but still need some more help.
> 
> Before I begin it would be best if I ask my questions in the context
> of a simple example. Lets say we have a simple Employee Administration
> application where there might a form that allows you to enter or edit
> employee information.
> 
> Now first off, it would seem likely using struts that I would have an
> EmployeeForm object and then maybe two action objects such as
> AddEmployee and EditEmployee (or possibly just one of them that can do
> all the tasks).
> 
> Now it would seem like for most situations you would have an
> EmployeeForm object and also an EmployeeBean. Now the question I have
> is at what point would you transfer the info in the EmployeeForm
> object to the EmployeeBean? Should this be done in the AddAction and
> then the AddAction might call a method in the business tier of
> doInsert( EmployeeBean employee )? Or do you possibly just pass all
> the form parameters off to a doInsert() method as arguments and then
> in the business tier doInsert() method do what you have to do with
> them.
> 
> I'm having some trouble seeing how they manage this with the
> SaveSubscriptionAction in the strut example. It looks like they are
> using PropertyUtils.copyProperties(subscription, subform); to get the
> information from the subform into a subscription object. (I admit I
> haven't studied using this PropertyUtils objects so I'll have to look
> into it). However, I'm still just generally confused about the best
> way to get the values in my EmployeeForm object which are all Strings
> into the correct format for being entered into a database.
> 
> Before looking into this struts framework, my controler servlet would
> pass the request object off to the appropriate Action class which in
> turn might hand the request object to another class method that would
> create an EmployeeBean from the request object parameters. Than after
> that EmployeeBean was returned the appropriate method in the business
> logic tier was called such as businessTierObject.addEmployee(
> employeeBean );
> 
> The validation abilities using FormObjects seem really powerful, yet
> I'm still stuck on where to do all my casting and conversions of the
> String request parameters into the proper data types (ie..birth date
> as a String to birthDate as a java.util.Date, ss# as a String to an
> int, etc).  I guess my basic question is where should this process
> take place? Do you pass the whole Form object in the Action object off
> to the business tier and let that level deal with creating the correct
> format of the request parameters?
> 
> I'm sure this has all been brought up and/or answered many times so I
> appreciate your patience. Is there a way I can search through past
> posts to this mailing list?
> 
> Thanks for any help.
> 
> --
> 
> Rick
> 
> mailto:maillist@reumann.net
> 
> "I can't stand cheap people. It makes me real mad when someone says
> something like, 'Hey, when are you going to pay me that $100 you owe
> me?' or 'Do you have that $50 you borrowed?' Man, quit being so
> cheap!"
>   -Jack Handey
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>

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


Re[2]: Best way to deal with Form object String parameters into another bean?

Posted by Rick Reumann <ma...@reumann.net>.
Thanks for the information Ted and your site has been very helpful to
me as well. Couple more comments below..

On Sunday, March 3, 2002, 9:18:25 AM, Ted Husted wrote:

TH> Do that, except you can create the EmployeeBean from the EmployeeForm
TH> instead. 

TH> If all the public properties you want to transfer are native types, and
TH> everything has been validated so you know it should convert, you can
TH> just do something like:

TH>  BeanUtils.populate(employeeBean,BeanUtils.describe(employeeForm));

     Do you have an example of this or a little more information on
     it? I'm a bit confused how it would operate. What would the
     describe method return to be used by this populate method in
     order to set the employeeBean with the employeeForm parameters?
     If you have some sample code or just the skeleton of how this
     populate method would work, I'd appreciate it.

     
TH> Or, you can do it the hard way, and just call getters inside of setters. 

TH> employeeBean.setProperty(new Integer(employeeForm.getProperty()));

    Right:) I know this way, the hard way:) I'm curious about doing
    that neat way you describe above but not sure how that
    implementation would look.
    
TH> If the properties don't match up exactly, you can used adapter methods
TH> that do whatever transformation is needed. For example, the form may put
TH> formatting characters in the property displayed to the user, but you may
TH> need to strip those out for the database. 

TH>     public void setTelephoneText(String telephone) {
TH>         setTelephone(ConvertUtils.getDigits(telephone));
TH>     }


TH> The databse would use setTelephone() and getTelephone(), a string of
TH> digits, but the form would use setTelephoneText and getTelephoneText,
TH> which format the string for display. The same can go for a property that
TH> needed to be localized, or a non-native type, like Date or Timestamp.

    Would you add these adapter methods right into the EmployeeForm
    object? I would assume so but just want to make sure. If that's
    the case... I'm wondering how bad it would be to set up an
    EmployeeForm object that would also create an EmployeeBean object.
    In other words each setter of the EmployeeForm object would also
    do what it had to do to change the datatype to the appropriate
    kind and then set that parameter in an EmployeeBean as a member of
    the EmployeeForm.. ie..

        setSocialSecNumber( String ssn ) {
            formSSN = ssn;
            //maybe validation now?
            if (validationOk) {
               employeeBean.setSSN( new Integer( ssn ) );
            }
        }

    Then of course there would be a method to return an EmployeeBean
    from the Form. With that said, though, the idea doesn't sit that
    well with me (the FormBean is just doing too much and it doesn't
    seem like the place for that kind of implementation. Maybe it's
    not so bad though).

    <END THIS REPLY/>
    
TH> If anyone wants to do more research, and summarize for the group, there
TH> are some starter links on the Newbie FAQ page (under construction,
TH> contributions welcome). 

TH> http://jakarta.apache.org/struts/newbie.html

TH> Something to keep in mind is that there will not be a single best
TH> practice. The ~best~ way to do a lot of these things depends on how the
TH> model is set up. Since Struts is model-neutral, the model can be set up
TH> anyway at all, and the part were the HTTP form data is transferred to
TH> the Model data bean can also be set up anyway at all. Sometimes the
TH> model may be set up to handle conversions from Strings (as ResultSets
TH> and RowSets are), and sometimes you need to do this yourself.

TH> -- Ted Husted, Husted dot Com, Fairport NY US
TH> -- Developing Java Web Applications with Struts
TH> -- Tel: +1 585 737-3463
TH> -- Web: http://husted.com/about/services


TH> Rick Reumann wrote:
>> 
>> I've just begun studying the struts examples and reading the
>> documentation and have waited a bit before I posted this question. I
>> am a newbie to struts but have done some searching and looking at
>> examples but still need some more help.
>> 
>> Before I begin it would be best if I ask my questions in the context
>> of a simple example. Lets say we have a simple Employee Administration
>> application where there might a form that allows you to enter or edit
>> employee information.
>> 
>> Now first off, it would seem likely using struts that I would have an
>> EmployeeForm object and then maybe two action objects such as
>> AddEmployee and EditEmployee (or possibly just one of them that can do
>> all the tasks).
>> 
>> Now it would seem like for most situations you would have an
>> EmployeeForm object and also an EmployeeBean. Now the question I have
>> is at what point would you transfer the info in the EmployeeForm
>> object to the EmployeeBean? Should this be done in the AddAction and
>> then the AddAction might call a method in the business tier of
>> doInsert( EmployeeBean employee )? Or do you possibly just pass all
>> the form parameters off to a doInsert() method as arguments and then
>> in the business tier doInsert() method do what you have to do with
>> them.
>> 
>> I'm having some trouble seeing how they manage this with the
>> SaveSubscriptionAction in the strut example. It looks like they are
>> using PropertyUtils.copyProperties(subscription, subform); to get the
>> information from the subform into a subscription object. (I admit I
>> haven't studied using this PropertyUtils objects so I'll have to look
>> into it). However, I'm still just generally confused about the best
>> way to get the values in my EmployeeForm object which are all Strings
>> into the correct format for being entered into a database.
>> 
>> Before looking into this struts framework, my controler servlet would
>> pass the request object off to the appropriate Action class which in
>> turn might hand the request object to another class method that would
>> create an EmployeeBean from the request object parameters. Than after
>> that EmployeeBean was returned the appropriate method in the business
>> logic tier was called such as businessTierObject.addEmployee(
>> employeeBean );
>> 
>> The validation abilities using FormObjects seem really powerful, yet
>> I'm still stuck on where to do all my casting and conversions of the
>> String request parameters into the proper data types (ie..birth date
>> as a String to birthDate as a java.util.Date, ss# as a String to an
>> int, etc).  I guess my basic question is where should this process
>> take place? Do you pass the whole Form object in the Action object off
>> to the business tier and let that level deal with creating the correct
>> format of the request parameters?
>> 
>> I'm sure this has all been brought up and/or answered many times so I
>> appreciate your patience. Is there a way I can search through past
>> posts to this mailing list?
>> 
>> Thanks for any help.
>> 
>> --
>> 
>> Rick
>> 
>> mailto:maillist@reumann.net
>> 
>> "I can't stand cheap people. It makes me real mad when someone says
>> something like, 'Hey, when are you going to pay me that $100 you owe
>> me?' or 'Do you have that $50 you borrowed?' Man, quit being so
>> cheap!"
>>   -Jack Handey
>> 
>> --
>> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>> For additional commands, e-mail: <ma...@jakarta.apache.org>

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




--

Rick

mailto:maillist@reumann.net

"As the light changed from red to green to yellow and back to red
again, I sat there thinking about life. Was it nothing more than a
bunch of honking and yelling? Sometimes it seemed that way."
  -Jack Handey


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