PDA

View Full Version : Accessing ApplicationContext from JSP Tags?


gzoller
Nov 23rd, 2004, 01:17 PM
Hello...

I have a bean 'Foo' that I need to be able to access from
outside the usual IoC machinery to be able to set property
values. I can't pull it out of Spring altogether because other
beans depend on it.

Specifically I want to be able to re-set the value of a property
in bean Foo in the code of a custom JSP tag I wrote. JSP tags
(so far as I can determine) get created and destroyed when
used, so cannot be pre-configured inside IoC (right?).

What I'd like to do inside my tag implementation code is
something like this:


// Inside my JSP tag implementation code...
XmlWebApplicationContext context = XmlWebApplicationContext.getInstance();
FooClass foo = (FooClass) context.getBean("Foo");
foo.setBlah("Hey!");
// Now new value of property blah is useable inside Spring
// app context beans that depend on Foo.


Unfortunately I haven't found where I can get to this kind of
"getInstance()" access to my app context.

Anyone know how to do this?

P.S. I did see ContextSingletonBeanFactoryLocator but wasn't
sure how to use it (or if this is the best way to get what I need).
I wanted to be sure that the context returned was in the same
context my Spring web-app is using so that the changes I make
to Foo in my JSP tag code will be visible in other app context
beans (i.e. the framework didn't just create a separate instance
of an app context using the same XML files!).

Thanks!
Greg

irbouho
Nov 23rd, 2004, 10:26 PM
You can use:
WebApplicationContextUtils.getWebApplicationContex t(pageContext.getServletContext());
from within your Tag Class. However I do not think it is a good idea to access Spring Context from jsp Tags. You can for example inject the bean inside a controller and add it to the Model then access it from the Tag.
HTH