You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by Colin Sampaleanu <co...@exis.com> on 2003/08/18 04:52:54 UTC

Any way to get ServletContext at time 'property-specifications' are processed?

Can anybody think of a way to the get ServletContext at the time that a 
page's <property-specification> elements are processed?

In the property-specification, I need to have an OGNL expression which 
calls a static member of a class, and feeds it the ServletContext, to 
get back a bean reference in return. Now I tried this for the expression:
    
@com.interface21.web.context.support.WebApplicationContextUtils@getWebApplicationContext(requestCycle.requestContext.applicationServlet.servletContext)

but unfortunately it doesn't work, since at the time 
property-specifications are processed, requestCycle's requestContext 
field is still null.

What I have done instead is overriden the Engine class, and stuck a 
reference to what I need in a global object, as follows:

  public static final String APPLICATION_CONTEXT_KEY = "appContext";
  protected void setupForRequest(RequestContext context) {
    super.setupForRequest(context);
   
    // insert ApplicationContext in global, if not there
    Map global = (Map) getGlobal();
    ApplicationContext ac = (ApplicationContext) 
global.get(APPLICATION_CONTEXT_KEY);
    if (ac == null) {
      ac = 
WebApplicationContextUtils.getWebApplicationContext(context.getServlet().getServletContext());
      global.put(APPLICATION_CONTEXT_KEY, ac);
    }
  }

and then in the property specification I use an OGNL expression like:

    global.appContext.getBean("myMapper")

This works, but what I don't like about this approach is that I have to 
have a custom Engine class, and have to use the Global. I guess it's not 
that big a deal, but I would still like to know if anybody can think of 
a way to get the ServletContext through OGNL at that time...

Regards,

Colin







RE: Any way to get ServletContext at time 'property-specifications' are processed?

Posted by "Howard M. Lewis Ship" <hl...@comcast.net>.
> @com.interface21.web.context.support.WebApplicationContextUtil
> s@getWebApplicationContext(requestCycle.requestContext.applica
> tionServlet.servletContext)
> 
> but unfortunately it doesn't work, since at the time 
> property-specifications are processed, requestCycle's requestContext 
> field is still null.
> 

That doesn't sound right; the requestCycle was probably null.

I was looking for a way to make this work; it will require changes to allow you to gain access the
requestcycle or requestcontext earlier.  Basically, the page is only attached to the engine and
request cycle after it is fully constructed, and that's after default values for specified
properties are set.

Please add a bug.