You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Michael Gagnon <mg...@genome.med.harvard.edu> on 2008/04/16 15:22:03 UTC

Advantage of prepare()?

I've been working with struts2 for about 2 months and so far I've been using
the preparable interface to populate any data the resulting page wants to
display. I've used the execute method to do non-visible work and determine a
result (which controls flow).

 

I'm wondering if this is the intended pattern. If so (and even if not), why?
The only intended benefit I see to using prepare at all is if you somehow
want to use one action in two different contexts - one which uses the
preparable interceptor and one which doesn't. If this was ever the case, I'd
argue two different actions should exist. If there's some common
functionality needed it should be in a superclass and the subclasses should
call into it in the execute method. So given the only appropriate scenario I
can think of off the top of my head, its use looks like an anti-pattern to
me.

 

 

I see prepare being used in the way I'm using it in many tutorials but I
haven't found any articles explaining any particular reason why. I'm hoping
maybe somebody can shed some light on this. should I bother using prepare at
all? Why?


Re: Advantage of prepare()?

Posted by Jukka Välimaa <va...@gmail.com>.
With preparable, you can load the objects you need in prepare() method, and
do whatever specific you want in the actual called method. In CRUD actions,
it is very handy to use prepare and paramsPrepareParams to first load the
stuff you want, and after prepare all the parameters are inserted
automatically. You don't need to change or access the object states
explixitly at all in many cases -- the second params interceptor in the
stack does that for you.

On Wed, Apr 16, 2008 at 4:22 PM, Michael Gagnon <
mgagnon@genome.med.harvard.edu> wrote:

> I've been working with struts2 for about 2 months and so far I've been
> using
> the preparable interface to populate any data the resulting page wants to
> display. I've used the execute method to do non-visible work and determine
> a
> result (which controls flow).
>
>
>
> I'm wondering if this is the intended pattern. If so (and even if not),
> why?
> The only intended benefit I see to using prepare at all is if you somehow
> want to use one action in two different contexts - one which uses the
> preparable interceptor and one which doesn't. If this was ever the case,
> I'd
> argue two different actions should exist. If there's some common
> functionality needed it should be in a superclass and the subclasses
> should
> call into it in the execute method. So given the only appropriate scenario
> I
> can think of off the top of my head, its use looks like an anti-pattern to
> me.
>
>
>
>
>
> I see prepare being used in the way I'm using it in many tutorials but I
> haven't found any articles explaining any particular reason why. I'm
> hoping
> maybe somebody can shed some light on this. should I bother using prepare
> at
> all? Why?
>
>

Re: Advantage of prepare()?

Posted by Dave Newton <ne...@yahoo.com>.
--- Michael Gagnon <mg...@genome.med.harvard.edu> wrote:
> I've been working with struts2 for about 2 months and so far I've been
> using the preparable interface to populate any data the resulting page 
> wants to display. I've used the execute method to do non-visible work 
> and determine a result (which controls flow).
> 
> I'm wondering if this is the intended pattern.

AFAIK, yes. Not sure what else it would be used for.

>  If so (and even if not), why?

It moves initialization code from the mainline code and provides a uniform,
deterministic way to run it across an application.

As a counter-example, consider a multi-developer project where each developer
initializes actions in a different way: some embed the code in an action's
execute() method, some call an initialization method in execute(), some use
an interceptor, ad nauseam. Preparable unifies the initialization mechanism.

> The only intended benefit I see to using prepare at all is if you somehow
> want to use one action in two different contexts - one which uses the
> preparable interceptor and one which doesn't.

Don't forget that Preparable isn't just the prepare() method, it's also
prepareXxxx() if you write actions with multiple methods. (Multiple methods
per action class is a cultural decision, and is debated from time to time.)

Consider also an action under unit test: it may not be desirable to call the
Preparable methods during testing, instead initializing the action manually
to allow testing of specific behaviors.

> I see prepare being used in the way I'm using it in many tutorials but I
> haven't found any articles explaining any particular reason why. I'm hoping
> maybe somebody can shed some light on this. should I bother using prepare
> at all? Why?

"Should" implies a level of obligation I'm not comfortable with.

I don't find the use of Preparable to be much of a productivity boost; the
"mainline" code savings are minimal, and it depends on framework knowledge to
understand why and how code using it works. I am ambivalent about its use,
but comfortable with either viewpoint.

Dave


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