You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Weissman, Alan" <al...@solspark.com> on 2003/06/18 17:07:31 UTC

Making Value Objects Into Form Objects

Ok so I'm a little new to Struts but not J2EE and I've been noticing that my
Value Objects that come out of EJB's in my data layer are the exact same
objects as the ActionForms that are used by Struts....and I'm sure I'm not
the first to notice this.

So here's my question:  How can I make one class that can serve as both an
ActionForm and a ValueObject?  

The problems with solutions that I've brainstormed are this:

1) I can't make my Value Objects extend ActionForm for obvious reasons.
2) I would rather not use an interface to get around this issue because then
I have two classes to maintain again.

Thanks for any advice!

Alan


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Making Value Objects Into Form Objects

Posted by Alen Ribic <al...@mweb.co.za>.
Also check description and known subclasses
http://jakarta.apache.org/struts/api/org/apache/struts/action/DynaActionForm
.html

--Alen


----- Original Message -----
From: "Alen Ribic" <al...@mweb.co.za>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Wednesday, June 18, 2003 5:15 PM
Subject: Re: Making Value Objects Into Form Objects


>
> ----- Original Message -----
> From: "Weissman, Alan" <al...@solspark.com>
> To: "'Struts Users Mailing List'" <st...@jakarta.apache.org>
> Sent: Wednesday, June 18, 2003 5:07 PM
> Subject: Making Value Objects Into Form Objects
>
>
> >
> > Ok so I'm a little new to Struts but not J2EE and I've been noticing
that
> my
> > Value Objects that come out of EJB's in my data layer are the exact same
> > objects as the ActionForms that are used by Struts....and I'm sure I'm
not
> > the first to notice this.
> >
> > So here's my question:  How can I make one class that can serve as both
an
> > ActionForm and a ValueObject?
>
> use org.apache.struts.action.DynaActionForm (Or one of the Dyna validator
> form classes)
> and define form-bean in struts config file.
>
> This way you will not need to define a ActionFrom for every form you work
> with.
>
> Then you use BeanUtils.copyProperties(...); method to copy bean properties
> from one to you value object.
>
> --Alen
>
> >
> > The problems with solutions that I've brainstormed are this:
> >
> > 1) I can't make my Value Objects extend ActionForm for obvious reasons.
> > 2) I would rather not use an interface to get around this issue because
> then
> > I have two classes to maintain again.
> >
> > Thanks for any advice!
> >
> > Alan
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: struts-user-help@jakarta.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Making Value Objects Into Form Objects

Posted by Alen Ribic <al...@mweb.co.za>.
----- Original Message -----
From: "Weissman, Alan" <al...@solspark.com>
To: "'Struts Users Mailing List'" <st...@jakarta.apache.org>
Sent: Wednesday, June 18, 2003 5:07 PM
Subject: Making Value Objects Into Form Objects


>
> Ok so I'm a little new to Struts but not J2EE and I've been noticing that
my
> Value Objects that come out of EJB's in my data layer are the exact same
> objects as the ActionForms that are used by Struts....and I'm sure I'm not
> the first to notice this.
>
> So here's my question:  How can I make one class that can serve as both an
> ActionForm and a ValueObject?

use org.apache.struts.action.DynaActionForm (Or one of the Dyna validator
form classes)
and define form-bean in struts config file.

This way you will not need to define a ActionFrom for every form you work
with.

Then you use BeanUtils.copyProperties(...); method to copy bean properties
from one to you value object.

--Alen

>
> The problems with solutions that I've brainstormed are this:
>
> 1) I can't make my Value Objects extend ActionForm for obvious reasons.
> 2) I would rather not use an interface to get around this issue because
then
> I have two classes to maintain again.
>
> Thanks for any advice!
>
> Alan
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Making Value Objects Into Form Objects

Posted by Sandeep Takhar <sa...@yahoo.com>.
This is similar to what we do.

We don't use copyProperties, but toDvo and fromDvo.

Just re-read Ted's book and he was talking about using
the formatting stuff on the Dvo and not the PDvo.

I haven't tried this, but it seems more right for some
reason.  Maybe it is business logic?

Maybe having a DateHolder and having this on the DVO
(or DTO if you call it that) and any other holders,
makes more sense...

I think I will try this next...

sandeep
--- Dave Ladd <da...@middlewarecg.com> wrote:
> We are using DynaForms and rather than define the
> attributes within
> struts-config, our approach to this problem was to
> create a PVO
> (presentation view objects) class which is basically
> a mirror of the VO but
> with the attributes defined as Strings.
> 
> The PVO is then defined as a property of a FormBean
> within the
> struts-config.  Population between the VO and PVO is
> done via
> BeanUtils.copyProperties.
> 
> By doing this, the size of the struts-config is
> reduced (probably not a big
> deal) and we gain some flexibility in being able to
> do any needed formatting
> within the PVO.  In addition, we quite often use
> these PVOs when we are
> building lists that are not part of the form itself.
>  We can also "mix and
> match" our PVOs within a FormBean which has proven
> helpful in a couple of
> situations.
> 
> The biggest drawback (that we are aware of) is that
> we have to create
> another class for each VO but this isn't much a
> hassle given the IDEs that
> are available.
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> struts-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Making Value Objects Into Form Objects

Posted by Dave Ladd <da...@middlewarecg.com>.
We are using DynaForms and rather than define the attributes within
struts-config, our approach to this problem was to create a PVO
(presentation view objects) class which is basically a mirror of the VO but
with the attributes defined as Strings.

The PVO is then defined as a property of a FormBean within the
struts-config.  Population between the VO and PVO is done via
BeanUtils.copyProperties.

By doing this, the size of the struts-config is reduced (probably not a big
deal) and we gain some flexibility in being able to do any needed formatting
within the PVO.  In addition, we quite often use these PVOs when we are
building lists that are not part of the form itself.  We can also "mix and
match" our PVOs within a FormBean which has proven helpful in a couple of
situations.

The biggest drawback (that we are aware of) is that we have to create
another class for each VO but this isn't much a hassle given the IDEs that
are available.


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Making Value Objects Into Form Objects

Posted by Erik Price <ep...@ptc.com>.

Adam Hardy wrote:
> You think one day HTTP will allow real types like int and date?

Let's hope not!  KISS.
You can use SOAP for something like that.

;)


Erik


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Making Value Objects Into Form Objects

Posted by Adam Hardy <ah...@cyberspaceroad.com>.
You think one day HTTP will allow real types like int and date?

Ted Husted wrote:
> Unless your value objects are expressly designed to act as 
> presentation-layer data transfer objects, you are liable to run into 
> problems when you try this in production.
> 
> In practice, ActionForms are the missing String buffer for the HTML 
> controls. It's the place where you can tuck the incoming Strings until 
> you have a chance to look at it and decide whether or not to carry it up 
> to the business layer.
> 
> If the ActionForm properties look like your value objects, it's because 
> the *HTML forms* look like your value objects. Eventually, one or the 
> others of these will start to change, and then you will be stuck.
> 
> In Struts 1.1, the DynaActionForms severely mitigate the issue of 
> maintaining two classes. You only need to maintain a lightweight XML 
> description of the HTML controls, and Struts will make that into a 
> JavaBean for you. Once you have vetted the Strings, you can then use 
> BeanUtil.CopyProperties to transfer and convert the validated input.
> 
> Before long, you may also find that you need to transfer the data to 
> more than one value object. In that case, you could add properties to 
> your DynaActionForm and transfer the properties to whatever value 
> objects need to know.
> 
> It is possible to use a business class for an ActionForm, if it was 
> designed to be used that way from the beginning. But that really only 
> works if the beans are being used as a data transfer object on its way 
> to a business facade.
> 
> If this is how your value objects where designed, then you could 
> implement the interface as an ActionForm, and then pass that to the facade.
> 
> But if you already have an implementation of the value objects, you 
> would probably be better off using the DynaActionForm. The next 
> alternative after that is to wrap the value object in an adaptor, but 
> that won't save you any maintenance.
> 
> A lot of people do feel that the ActionForm should be an interface. 
> Since more and more new applications are being designed with MVC in 
> mind, this does start to make some sense. But, Craig would veto that as 
> a product change, so using an interface for an input form could only be 
> done using alternative RequestProcessor or something.
> 
> -Ted.
> 
> Weissman, Alan wrote:
> 
>> Ok so I'm a little new to Struts but not J2EE and I've been noticing 
>> that my
>> Value Objects that come out of EJB's in my data layer are the exact same
>> objects as the ActionForms that are used by Struts....and I'm sure I'm 
>> not
>> the first to notice this.
>>
>> So here's my question:  How can I make one class that can serve as 
>> both an
>> ActionForm and a ValueObject? 
>> The problems with solutions that I've brainstormed are this:
>>
>> 1) I can't make my Value Objects extend ActionForm for obvious reasons.
>> 2) I would rather not use an interface to get around this issue 
>> because then
>> I have two classes to maintain again.
>>
>> Thanks for any advice!
>>
>> Alan
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>>
>>
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Making Value Objects Into Form Objects

Posted by Ted Husted <hu...@apache.org>.
Unless your value objects are expressly designed to act as 
presentation-layer data transfer objects, you are liable to run into 
problems when you try this in production.

In practice, ActionForms are the missing String buffer for the HTML 
controls. It's the place where you can tuck the incoming Strings until 
you have a chance to look at it and decide whether or not to carry it up 
to the business layer.

If the ActionForm properties look like your value objects, it's because 
the *HTML forms* look like your value objects. Eventually, one or the 
others of these will start to change, and then you will be stuck.

In Struts 1.1, the DynaActionForms severely mitigate the issue of 
maintaining two classes. You only need to maintain a lightweight XML 
description of the HTML controls, and Struts will make that into a 
JavaBean for you. Once you have vetted the Strings, you can then use 
BeanUtil.CopyProperties to transfer and convert the validated input.

Before long, you may also find that you need to transfer the data to 
more than one value object. In that case, you could add properties to 
your DynaActionForm and transfer the properties to whatever value 
objects need to know.

It is possible to use a business class for an ActionForm, if it was 
designed to be used that way from the beginning. But that really only 
works if the beans are being used as a data transfer object on its way 
to a business facade.

If this is how your value objects where designed, then you could 
implement the interface as an ActionForm, and then pass that to the facade.

But if you already have an implementation of the value objects, you 
would probably be better off using the DynaActionForm. The next 
alternative after that is to wrap the value object in an adaptor, but 
that won't save you any maintenance.

A lot of people do feel that the ActionForm should be an interface. 
Since more and more new applications are being designed with MVC in 
mind, this does start to make some sense. But, Craig would veto that as 
a product change, so using an interface for an input form could only be 
done using alternative RequestProcessor or something.

-Ted.

Weissman, Alan wrote:
> Ok so I'm a little new to Struts but not J2EE and I've been noticing that my
> Value Objects that come out of EJB's in my data layer are the exact same
> objects as the ActionForms that are used by Struts....and I'm sure I'm not
> the first to notice this.
> 
> So here's my question:  How can I make one class that can serve as both an
> ActionForm and a ValueObject?  
> 
> The problems with solutions that I've brainstormed are this:
> 
> 1) I can't make my Value Objects extend ActionForm for obvious reasons.
> 2) I would rather not use an interface to get around this issue because then
> I have two classes to maintain again.
> 
> Thanks for any advice!
> 
> Alan
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 
> 


-- 
Ted Husted,
Struts in Action <http://husted.com/struts/book.html>



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Making Value Objects Into Form Objects

Posted by Adam Hardy <ah...@cyberspaceroad.com>.
Hi Alan,
you could place the value bean in the form with a setValueObject() 
method on the form.

In your JSP you could use indexed tags to get to the bean, e.g. for 
property getTitle()

<input id="mybean[0].title">

Adam

Weissman, Alan wrote:
> Ok so I'm a little new to Struts but not J2EE and I've been noticing that my
> Value Objects that come out of EJB's in my data layer are the exact same
> objects as the ActionForms that are used by Struts....and I'm sure I'm not
> the first to notice this.
> 
> So here's my question:  How can I make one class that can serve as both an
> ActionForm and a ValueObject?  
> 
> The problems with solutions that I've brainstormed are this:
> 
> 1) I can't make my Value Objects extend ActionForm for obvious reasons.
> 2) I would rather not use an interface to get around this issue because then
> I have two classes to maintain again.
> 
> Thanks for any advice!
> 
> Alan
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Making Value Objects Into Form Objects

Posted by florian <cs...@structbench.com>.


hi!

its funny how this thing comes up every two weeks on the list =)

i got to admit that at first the solution of having value objects/dtos
in an actionform sounded great and making things easy in the
action.. no more endless accessor calling..

but after thinking about it for a while and some playing around
with the different possible solutions i figured two things:

1) i misunderstood/misused what actionforms are. it seems like
some kind of hack to expose the vos so far out to the client layer.

2) things get ugly when you want to redisplay invalid input 'foo22'
for an int property for example..


ciao!
florian



On Mittwoch, Juni 18, 2003, at 05:07  Uhr, Weissman, Alan wrote:

>
> Ok so I'm a little new to Struts but not J2EE and I've been noticing 
> that my
> Value Objects that come out of EJB's in my data layer are the exact 
> same
> objects as the ActionForms that are used by Struts....and I'm sure I'm 
> not
> the first to notice this.
>
> So here's my question:  How can I make one class that can serve as 
> both an
> ActionForm and a ValueObject?
>
> The problems with solutions that I've brainstormed are this:
>
> 1) I can't make my Value Objects extend ActionForm for obvious reasons.
> 2) I would rather not use an interface to get around this issue 
> because then
> I have two classes to maintain again.
>
> Thanks for any advice!
>
> Alan
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Making Value Objects Into Form Objects

Posted by Blake Whitmore <bl...@yahoo.com>.
Alan,

A good tool for this is the
org.apache.commons.beanutils.BeanUtils.copyProperties()
method.

http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/BeanUtils.html




--- "Weissman, Alan" <al...@solspark.com> wrote:
> 
> Ok so I'm a little new to Struts but not J2EE and
> I've been noticing that my
> Value Objects that come out of EJB's in my data
> layer are the exact same
> objects as the ActionForms that are used by
> Struts....and I'm sure I'm not
> the first to notice this.
> 
> So here's my question:  How can I make one class
> that can serve as both an
> ActionForm and a ValueObject?  
> 
> The problems with solutions that I've brainstormed
> are this:
> 
> 1) I can't make my Value Objects extend ActionForm
> for obvious reasons.
> 2) I would rather not use an interface to get around
> this issue because then
> I have two classes to maintain again.
> 
> Thanks for any advice!
> 
> Alan
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> struts-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org