views:

1391

answers:

3

I would like to put two small constant objects (one TreeMap and one ArrayList) somewhere in the ServletContext of a Struts2 webapp (running under Tomcat 6), such that those objects may be accessible from any JSP, through the Application Scope.

Under Struts1, I would have used one of those startup "plug-ins" (defined in struts-config.xml). Is there an equivalent way of achieving the same result with Struts2?

Alternatively (and maybe even better), since my webapp uses Spring configured in Tomcat with Spring's ContextLoaderListener, can I rely on Spring instead? For instance by creating a bean that declares some appropriate "ApplicationAware" interface...

A: 

If this was my application (and we're using a fairly similar architecture), I'd inject a singleton spring bean into the base class of my struts2 controller class, but all my jsp's have controllers and all share a common base class.

krosenvold
To be honest, I love Struts2, but I avoid OGNL as much as I can (too slow!)---prefering to rely on EL if possible. Also, I would rather not re-inject the same constant objects on the ValueStack with each new request through an interceptor... (That's what you had in mind, isn't it?)
pierdeux
A: 

Take 2: Implement the ServletContextAware interface on one of your spring beans and just do the necessary modifications to the context. If necessary do it in a @PostConstruct annotated method because then your bean is complete at the time you update the servletcontext.

krosenvold
I hadn't thought of the @PostConstruct annotation. Nice touch! Thanks!
pierdeux
+1  A: 

Take 3: You might want to check out Spring Servlet Context scope

krosenvold