Dan, at the time your field initializer runs the ActionBean has not been
fully instantiated. Until the constructor completes and Stripes gets a
reference to the new ActionBean, Stripes can't call setContext(..) on it.
That's why there is no context. There are several things you can do to make
it work for you.
1. Lazy initialization in your getter. E.g.:
public Firm getFirm() {
if (firm == null) {
firm = (Firm)
getContext().getDataContext().newObject(Firm.class);
}
return firm;
}
2. A @Before or @After method. E.g.:
@After(stages=LifecycleStage.ActionResolution)
public void init() {
firm = (Firm) getContext().getDataContext().newObject(Firm.class);
}
3. Initialize the fields within your event handler, like you showed in
your original message.
4. Override setContext(..) and initialize your fields there. You'll know
for sure you have a context then.
Those are listed in the order I prefer them, personally. I know many folks
would swap 1 and 2.
-Ben
On Fri, Feb 27, 2009 at 9:34 AM, Dan Cane <[email protected]> wrote:
> Greet all -
>
> First off, a truly wonderful framework here. I'm a huge fan of how quick
> effective stripes truly is.
>
> Here's my newbish question. I've selected Apache Cayenne for my ORM
> framework
> for a number
> of reasons, and I'm stumped as to how to "reuse" my database context. From
> the
> cayenne docs
> located at http://cayenne.apache.org/doc/web-applications.html I gather
> that
> instead of creating a
> new database context inside each bean (which works, but as you can imagine
> is
> not a good
> performance pattern) I should be using the magic "cayenne voodoo which can
> re-use the
> database context attached to the HttpSession object."
>
> ** prepare yourself for a very newb statement that I'm hopeful someone out
> there
> is willing to handhold me through **
>
> Ok - so I get that I'm supposed to get the dataContext from the Util which
> can
> pop it off session:
>
> HttpSession session = ...;
> DataContext context = ServletUtil.getSessionContext(session);
>
> So - I figure I already extended ActionBean and ActionBeanContext so why
> not
> just put a method
> in my new context to get the data context..
>
> -snip-
> public class BKFActionBeanContext extends ActionBeanContext {
>
> public void setUser(User currentUser) {
> getRequest().getSession().setAttribute("user", currentUser);
> }
>
> public User getUser() {
> return (User)
> getRequest().getSession().getAttribute("user");
> }
>
> public DataContext getDataContext() {
> return
> ServletUtil.getSessionContext(getRequest().getSession());
> }
>
> /** Logs the user out by invalidating the session. */
> public void logout() {
> getRequest().getSession().invalidate();
> }
>
> }
> -snip-
>
> Ok - and lo-and-behold it WORKS (sometimes). I found that inside of things
> like
> Validate methods
> and Resolutions I can quite easily use getContext().getDataContext() and it
> has
> exactly what I need.
>
> BUT -- (someone smack me for this) when I attempt to use the data context
> while
> declaring a new
> variable for my object I find that getContext (stripes) returns null, so of
> course my
> getContext().getDataContext() throws a NPE exception.
> (I really should have taken classes in school since I'm sure someone can
> explain
> why that's
> a no-no)
>
> e.g
> -- this is not working --
> @UrlBinding("/RegisterFirm.action")
> public class RegisterFirmActionBean extends BKFActionBean {
>
> @ValidateNestedProperties({
> @Validate(...),
> })
> private Firm firm = (Firm)
> getContext().getDataContext().newObject(Firm.class);
> -- end what throws a NPE --
>
> Ok - so I've on day 2 of trying things and I'm at the point of asking for
> help.
> Help. :)
>
> Is there a better/different way to new up a "Firm" object and then set the
> object properties,
> e.g. in my Resolution do a
> @DefaultHandler
> public Resolution view() {
> this.firm = (Firm)
> getContext().getDataContext().newObject(Firm.class);
> return new ForwardResolution("/RegisterFirm.jsp");
> }
>
> or something else all together.
> I would think it's desirable to have my data context on the session, but...
>
> Anyways, THANK YOU for taking some time to help me out.
>
> Dan
>
------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Stripes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-users