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

gla*_*666 15 java spring spring-3

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

<context:property-placeholder  location="classpath:application.properties" />
Run Code Online (Sandbox Code Playgroud)

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

${something.myProperty}?
Run Code Online (Sandbox Code Playgroud)

sin*_*pop 38

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>
Run Code Online (Sandbox Code Playgroud)

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


btp*_*ka3 6

在Spring 3.1之后,您可以像以下一样使用SpEL<spring:eval />标记:

<spring:eval expression="@applicationProps['application.version']" 
             var="applicationVersion"/>
Run Code Online (Sandbox Code Playgroud)