2012-10-17

Accessing the ServletContext in Tapestry 4.1

We use Tapestry 4.1 at my new gig, so I decided to implement my sandbox webapp with it to improve my understanding; this process has helped me immensely.

In this webapp I have a gallery that is built dynamically based on a listing of files from a directory within the app context. It was a bit of a struggle to figure out how to get access to the servlet context, but it's incredibly easy once you know the incantation.
    @InjectObject("service:tapestry.globals.ServletContext")
    public abstract ServletContext getServletContext();
That's it. Using this method, the @InjectObject annotation signals Hivemind to inject the servlet context into the concrete page class generated at runtime.

To access the context, simply call getServletContext() or whatever you name the abstract getter into which you inject the context object.

A full list of what Hivemind calls 'service points' can be found at the tapestry framework hivedoc.

No comments:

Post a Comment