I thought I'd quickly describe my usages and opinions of Preparable and
ModelDriven in case it helps any one:
- As Dave described, I also prefer to keep my prepare logic with the
main code because it's easier for others to understand.
- I do use Preparable in the cases where params-prepare-params is
desirable: typically where the params are directly addressing a nested
bean and the parent needs to be initialised/loaded by id first. I avoid
this but its valuable in those occasional cases.
- When using ModelDriven, putting any logic inside getModel() is a bad
idea. This is akin to putting logic inside getters. The model is
always an instance variable.
- My interpretation of the primary objective of ModelDriven is to put
the model object at the top of the value stack rather than the action.
Therefore it is complementary with Preparable
- My use of ModelDriven is also rare because it can make the context of
a JSP/FTL harder to understand (eg. I prefer contact.firstName rather
than firstName). However it has proven extremely useful in some cases,
particularly if the model is polymorphic (same action, different result,
different model class)
cheers,
Jeromy Evans
Musachy Barroso wrote:
Struts will by default populate parameters in your action, if you use
ModelDriven, then you can supply the object that will be populated
instead of the action. Implementing preparable will make struts call
prepare() before the action is executed, or prepareX() before X() is
executed. They are just sometimes used together to achieve certain
things, as explained on the other emails.
musachy
On Jan 31, 2008 5:03 PM, Adam Hardy <[EMAIL PROTECTED]> wrote:
I don't think you are strictly conveying the intended usage, when you say that
the getModel() method will 'later' return the model. It depends how your
interceptor stack is ordered. In the docs, it explicitly says the
ModelDrivenInterceptor should come before ParametersInterceptor, as often the
PrepareInterceptor does.
Both ModelDriven- and PrepareInterceptor can retrieve the model and place it in
instance variables on the action.
http://struts.apache.org/2.0.11/docs/prepare-interceptor.html
http://struts.apache.org/2.0.11/docs/model-driven-interceptor.html
What I originally wanted to ask (whether I conveyed my question correctly is
another matter) was: what's the difference in intention and implementation
between these two?
Thanks
Adam
Roberto Nunnari on 31/01/08 18:12, wrote:
The prepare() method task is to retrive the model
and store it as an instance variable.
The getModel() method will later return the model
when asked by the framework that will put it on the
value stack.
Please correct me if I'm wrong.
Adam Hardy wrote:
Dave Newton on 31/01/08 16:00, wrote:
Adam Hardy <[EMAIL PROTECTED]> wrote:
Dave Newton on 31/01/08 15:29, wrote:
Adam Hardy <[EMAIL PROTECTED]> wrote:
* ModelDriven: how do I get hold of my model during my action
methods? I
don't want to get getModel() again, because that will retrieve
another
instance of the entity and not the one that was populated by struts.
Lazy initialization (via null check or by setting an instance var), or
config through Spring, or...
If your getModel() puts the model entity on an instance variable, won't
that confuse the param interceptor?
Depends on what instance variable you put it in, I suppose. One of your
questions was how to access the model object w/o running through a
getModel()
method that would re-instantiate; lazily initializing an instance
variable in
getModel() then using that instance variable is one way of dealing
with that.
OK, that makes sense now, thanks. What I thought would be the more
obvious way of obtaining the model would be to fetch it from where
struts puts it when it calls getModel(), i.e. the value stack, however
I don't see any access methods for doing that, particulary on
ActionSupport.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]