Results 1 to 2 of 2

Thread: Accessing ApplicationContext from JSP Tags?

  1. #1
    Join Date
    Nov 2004
    Posts
    5

    Default Accessing ApplicationContext from JSP Tags?

    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:

    Code:
    // 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

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    You can use:
    Code:
    WebApplicationContextUtils.getWebApplicationContext(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
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

Similar Threads

  1. Replies: 4
    Last Post: Jun 8th, 2005, 06:22 AM
  2. Replies: 2
    Last Post: May 29th, 2005, 05:22 AM
  3. Obtaining ApplicationContext for web and batch apps
    By Thomas Vaught in forum Container
    Replies: 10
    Last Post: May 23rd, 2005, 07:09 AM
  4. Replies: 8
    Last Post: Apr 11th, 2005, 02:33 PM
  5. Accessing the ApplicationContext directly...
    By Dan Washusen in forum Container
    Replies: 10
    Last Post: Jan 4th, 2005, 10:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •