Results 1 to 3 of 3

Thread: Bug in Struts ContextLoaderPlugIn?

  1. #1
    Join Date
    Aug 2004
    Location
    California
    Posts
    17

    Default Bug in Struts ContextLoaderPlugIn?

    The documented method of obtaining a reference to the WebApplication context is:
    Code:
      WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    Unfortunately, this does not work when using the Struts ContextLoaderPlugIn. The workaround is the following:
    Code:
    WebApplicationContext webApplicationContext = (WebApplicationContext)
            servletContext.getAttribute(ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX);
    Can the ContextLoaderPlugIn be modified to support the documented method? It should be a simple one-line change and would have saved a lot of time.

  2. #2
    Join Date
    Aug 2004
    Location
    Linz, Austria
    Posts
    391

    Default

    Well, the ContextLoaderPlugIn context is not meant to be accessed that way, because it is not equal to the root application context: It is mainly intended for hosting Struts Action instances and cooperating objects for them. Generic business objects and other reusable stuff that does not depend on Struts should go into a true root application context.

    For example, if you have a ContextLoaderListener (root context defined in web.xml) and a ContextLoaderPlugIn (defined in struts-config), the latter will automatically be a child of the former. This means that beans in the ContextLoaderPlugIn context can easily reference beans in the ContextLoaderListener context, but not the other way round.

    So if you've got the need to access beans that are defined in ContextLoaderPlugIn context directly via a WebApplicationContext reference, I recommend to move those beans over to a ContextLoaderListener context and access them via WebApplicationContextUtils. Just keep Struts-specific beans in the ContextLoaderPlugIn context.

    Juergen

    P.S.:
    If you absolutely need to access the ContextLoaderPlugIn context, you can use the getRequiredWebApplicationContext method in the DelegatingActionUtils class. Note that this method is not really meant to be used directly in application code, though.

  3. #3
    Join Date
    Aug 2004
    Location
    California
    Posts
    17

    Default

    Quote Originally Posted by Juergen Hoeller
    Well, the ContextLoaderPlugIn context is not meant to be accessed that way.
    Thanks for the clarification - I had erroneously assumed that the ContextLoaderPlugIn was intented to be a full alternative.

Similar Threads

  1. Spring MVC Web Framework versus Struts
    By biguniverse in forum Web Flow
    Replies: 27
    Last Post: Aug 29th, 2012, 03:57 AM
  2. Replies: 4
    Last Post: Aug 1st, 2005, 03:45 PM
  3. Replies: 2
    Last Post: May 24th, 2005, 11:22 AM
  4. Replies: 0
    Last Post: Dec 11th, 2004, 07:57 AM
  5. Replies: 10
    Last Post: Nov 2nd, 2004, 09:38 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
  •