在我的应用程序上下文中,我定义了属性文件:
<context:property-placeholder location="classpath:application.properties" />
我想获取JSP页面上该文件中定义的属性的值。有没有办法做到这一点
${something.myProperty}?
PropertyPlaceholderConfigurer只能解析Spring配置中的占位符(XML或注释)。在Spring应用程序中使用Propertiesbean 是非常普遍的。你可以通过这种方式从视图中访问它(假设你正在使用InternalResourceViewResolver):
PropertyPlaceholderConfigurer
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']}。
${properties.myProperty}
${properties['my.property']}