Announcement Announcement Module
Collapse
No announcement yet.
Exposing Properties from PropertyPlaceholderConfigurer to JSPs Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Exposing Properties from PropertyPlaceholderConfigurer to JSPs

    First off, a little disclaimer: I've been tasked with bugfixing/improving a system that's been developed over the last 10 years or so, using a (dis-)array of different technologies and by different developers, not all of which have done what you'd call a smashing job. In addition, we're missing the source for parts of the system. Therefore, I have to find ways of improving the state of things that can co-exist alongside the current implementation. So, I know that I'm asking things that may make you cringe, but with the state of things, there are really no other options.

    What I need to do is expose properties read from property-files to the view. The view mainly consists of good, ol' JSPs (ie, there's no Spring MVC or servlets feeding the data). The culprit is that these property-files have placeholders, and I cannot for the life of me make Spring resolve the placeholders when using the properties in the view.

    Personally, I'd've thought this would work:
    Code:
        <context:property-placeholder properties-ref="appProperties"/>
        <util:properties id="appProperties" location="classpath:server.properties" />
    And, then, in the JSPs, go:
    Code:
        ApplicationContext context = (ApplicationContext) application.getAttribute("org.springframework.web.context.WebApplicationContext.ROOT");
        Properties springUtilProps = context.getBean("appProperties", Properties.class);
    But, as stated, none of the placeholders get resolved, leaving me with "Property value: ${testProp}" instead of "Property value: Property resolved".

    Is there any way of achieving what I want?

  • #2
    No... At least not with your current approach... A PropertyPlaceHolderConfigurer only operates on beans (well actually BeanDefinition) not in JSP of whatever... So trying to replace placeholders inside JSPs is never going to work, the only way to achieve what you want is to pull out the properties yourself.

    Also note that it is better to use the WebAPplicationContextUtils to get access to the ApplicationContext insead of using a getAttribute.

    Comment


    • #3
      I appreciate your answer, and will try another venue since I've spent way too much time trying to make it work this way.

      Would you mind elaborating a bit on the "pull out the properties yourself"-bit? Basically, I need some pointers on how to attack it from that angle.

      Again, thanks

      Comment


      • #4
        Well just as I said it .

        Code:
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(application);
        Properties props = context.getBean("appProperties", Properties.class);
        String yourProperty = props.getProperty("yourProperty");
        Now you can do anything you like with the property.

        Code:
        Property value: <%=yourProperty%>

        Comment


        • #5
          I must be misunderstanding something here (common occurence, that). But, if I keep my application-context.xml as in my first post, and do what you say, I get a
          Code:
          org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'propPlaceHolder' must be of type [java.util.Properties], but was actually of type [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer]
          However, for some reason I really can't understand, this doesn't happen if I use
          Code:
          ApplicationContext context = (ApplicationContext) application.getAttribute("org.springframework.web.context.WebApplicationContext.ROOT");
          Properties springUtilProps = context.getBean("appProperties", Properties.class);
          It then just gives me the property I ask for, but with the placeholder unresolved.

          Comment


          • #6
            From my point-of-view you are comparing apples and oranges...

            Your first attempt is that you are trying to retrieve a bean called 'propPlaceHolder' which is obviously not a java.util.Properties object but a PropertyPlaceHolderCOnfigurer (as the error states). In your second approach you get the properties object that is loaded by the utilroperties object.

            However I doubt we are going to understand each other as we are probably talking into circles. For some reason I must be misunderstanding what you want, however one thing is clear you are never going to resolve placeholders in JSP files as spring will only replace them in Spring beans.

            Comment

            Working...
            X