Loading an object in ServletContext (application scope)
Question
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-in
s" (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...
Solution
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.
OTHER TIPS
Take 3: You might want to check out Spring Servlet Context scope
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.