【问题标题】:How to show values from property file in JSP in a spring MVC app如何在 Spring MVC 应用程序中的 JSP 中显示属性文件中的值
【发布时间】:2013-02-13 05:03:54
【问题描述】:

我在app-servlet.xml 中使用这样的bean 设置我的属性:

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="/WEB-INF/my.properties"></property>
    </bean>

大多数时候我访问我的控制器或其他类中的属性,如下所示:

@Value("${dbtype}")
public String dbType;

但是如果我想使用 JSP 文件中的属性并绕过控制器怎么办。这意味着我不希望将值类型作为模型属性从控制器传递到 JSP。

有没有办法直接在 jsp 中访问属性?

【问题讨论】:

标签: java spring jsp spring-mvc


【解决方案1】:

弹簧配置

<util:properties id="propertyConfigurer" 
                  location="classpath:yourPropertyFileClasspathHere "/>
<context:property-placeholder properties-ref="propertyConfigurer" />

jsp

<spring:eval expression="@propertyConfigurer.getProperty('propertyNameHere')" />

【讨论】:

  • 你必须添加spring taglib到页面:springframework.org/tags" %>
  • 在较新版本的 spring 上使用 propertyConfigurer 为我工作。
【解决方案2】:

您还可以执行的操作不会限制您在单个属性占位符中查找属性,或者如果您使用 java 配置并且仅实例化 PropertySourcesPlaceholderConfigurer 是使用环境对象:

<spring:eval expression="@environment.getProperty('application_builtBy')" />

【讨论】:

  • 我觉得这个更有帮助。谢谢
【解决方案3】:
<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>

【讨论】:

  • 感谢您指定标签库声明。这经常被省略。
【解决方案4】:

在上下文中这样做:

<util:properties 
    id="propertyConfigurer"
    location="classpath:yourPropertyFileClasspathHere"
/>
<context:property-placeholder properties-ref="propertyConfigurer" />

用于创建 Properties bean(与 @nkjava.blogspot.com 在他的 answer 中相同)。 但这并不是所有需要做的工作。

现在您需要将此 bean 公开给 JSP。 有几种方法可以做到这一点,取决于视图解析器的类型。 InternalResourceViewResolver 有解决方案 - 您需要将“exposeContextBeansAsAttributes”设置为 true 并使用所需 bean 的列表填充“exposedContextBeanNames”。

对于tiles也有解决办法。

你可以简单地在你的 JSP 中使用这个 bean。以 EL 为例:

${propertyConfigurer['my.string.from.prop.file']}

【讨论】:

    【解决方案5】:

    在 Spring 版本 4 中,您可以找到属性文件:

    1) xml模式

                    <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
                       <property name="ignoreUnresolvablePlaceholders" value="true"/>
                          <property name="locations">
                              <list>
                                <!-- default resources folder (default package maven project) -->
                                 <value>classpath:mongodb.remote.properties</value>  
                                    <!-- Or in /WEB-INF/ folder -->
                                  <value>/WEB-INF/mongodb.remote.properties</value>  
                              </list>
                          </property>
                      </bean>
    ----------------------------------------------------------------------------------------
    

    2) 程序化模式:

        If you have for example this package : com.profile.config, com.profile.controller, ecc.. 
        it's not problem if you put only com.profile, it's ok !!! Now
    
    
        @Configuration
        @ComponentScan(basePackages = "com.profile")
        /** resources folder & default package maven project*/
        @PropertySource(value = { "classpath:mongodb.remote.properties" }) 
        public class MyPropertySourcesPlaceholderConfigurer {
    
    
            @Bean
            public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
                return new PropertySourcesPlaceholderConfigurer();
            }
        }
    
    
        ---------------------------------------------------------------------------------
        Your property file
    
        label.test.val=this is the property file value!!!!!
        ---------------------------------------------------------------------------------
    
        @Controller
        public class LabelsAndValuesController {
    
    
             @Value("${label.test.val}")
             String test;
    
        }
    

    输出:

        ---------------------------------------------------------------------------------
        this is the property file value!!!!!
        ---------------------------------------------------------------------------------
    

    【讨论】:

      猜你喜欢
      • 2016-07-18
      • 2013-03-23
      • 2014-09-19
      • 1970-01-01
      • 2014-10-29
      • 2015-03-04
      • 2017-10-25
      • 2013-06-06
      • 1970-01-01
      相关资源
      最近更新 更多