【问题标题】:How to use property from property file specified in PropertyPlaceholderConfigurer in JSP如何使用 JSP 中 PropertyPlaceholderConfigurer 中指定的属性文件中的属性
【发布时间】:2011-04-25 10:25:04
【问题描述】:

在我的应用程序上下文中,我定义了属性文件:

<context:property-placeholder  location="classpath:application.properties" />

我想获取 JSP 页面上该文件中定义的属性的值。有没有办法做到这一点

${something.myProperty}?

【问题讨论】:

    标签: java spring spring-3


    【解决方案1】:

    PropertyPlaceholderConfigurer 只能解析 Spring 配置中的占位符(XML 或注解)。在 Spring 应用程序中很常见使用 Properties bean。您可以通过这种方式从您的视图中访问它(假设您使用的是InternalResourceViewResolver):

    <bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list><value>classpath:config.properties</value></list>
        </property>
    </bean>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
        <property name="exposedContextBeanNames">
            <list><value>properties</value></list>
        </property>
    </bean>
    

    然后,在您的 JSP 中,您可以使用 ${properties.myProperty}${properties['my.property']}

    【讨论】:

    • 真的很好!但是声明中的第一个 bean 是 PropertiesFactoryBean 类型,而不是 ProperyPlaceholderConfigurer。这是否意味着为了替换 xml 中的属性占位符,我要在 PropertyPlaceholderConfigurerBean 中重复声明应用程序属性?
    • @glaz666:我忘了说 PropertyPlaceholderConfigurer 不适合这个。我稍微修改了答案。
    • 我已经将这个“属性”bean 传递给 PlaceHolderConfigurer 并且它似乎可以工作,但我仍然无法使其在 JSP 文件中工作,因为当我尝试访问 ${properties} 时它甚至没有尝试从发生 bean 暴露的 ContextExposingHttpServletRequest 调用 getAttribute
    • @glaz666:奇怪的行为......你使用的是哪个版本的 Spring MVC? ${...} 适用于其他对象(例如您的控制器结果)?
    • @VishalZanzrukia Properties 实现了 Map,所以你可以使用 ${properties['i.want.chicken.now']}
    【解决方案2】:

    在 Spring 3.1 之后,你可以像这样使用&lt;spring:eval /&gt; 标签和SpEL

    <spring:eval expression="@applicationProps['application.version']" 
                 var="applicationVersion"/>
    

    【讨论】:

      【解决方案3】:

      与列表中可能不存在的多个位置一起使用,可以使用 context:property-placeholder bean:

      <beans:bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
          <beans:property name="ignoreResourceNotFound" value="true" />
          <beans:property name="locations">
              <beans:list>
                  <beans:value>classpath:application.properties</beans:value>
                  <beans:value>classpath:environment.properties</beans:value>
                  <beans:value>classpath:environment-${env}.properties</beans:value>
              </beans:list>
          </beans:property>
      </beans:bean>
      

      【讨论】:

        【解决方案4】:

        要在视图中使用递归属性占位符扩展,您需要一个不同的解决方案,看看这个答案:

        https://stackoverflow.com/a/10200249/770303

        【讨论】:

          【解决方案5】:
          `<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" 
            id="messageSource"
            p:basenames="WEB-INF/i18n/site"
            p:fallbackToSystemLocale="false"/>`
          

          现在这是您的属性文件

          `site.name=Cool Bananas`
          

          而且。 这是您的 JSP

          `<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
          <html>
            <head>
              <title><spring:message code="site.name"/></title>
            </head>
            <body>
            </body>
          </html>`
          

          【讨论】:

            【解决方案6】:

            这将向您显示当前架构的表(您已登录):

            select table_name from user_tables order by table_name;
            

            这将显示您至少拥有选择权限的 schema 表:

            select owner, table_name from all_tables where owner='<owner>' order by owner, table_name;
            

            【讨论】:

              猜你喜欢
              • 2010-12-08
              • 2013-06-30
              • 2012-03-24
              • 1970-01-01
              • 2015-06-06
              • 1970-01-01
              • 2015-10-28
              • 1970-01-01
              • 2011-07-09
              相关资源
              最近更新 更多