小编典典

如何使用JSP的PropertyPlaceholderConfigurer中指定的属性文件中的属性

spring

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

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

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

${something.myProperty}?

阅读 330

收藏
2020-04-20

共1个答案

小编典典

PropertyPlaceholderConfigurer只能解析Spring配置中的占位符(XML或注释)。在Spring应用程序中使用Propertiesbean 是非常普遍的。你可以通过这种方式从视图中访问它(假设你正在使用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']}

2020-04-20