如何使用JSP的PropertyPlaceholderConfigurer中指定的属性文件中的属性 [英] How to use property from property file specified in PropertyPlaceholderConfigurer in JSP

查看:132
本文介绍了如何使用JSP的PropertyPlaceholderConfigurer中指定的属性文件中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my application context I have defined properties file:

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

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

I want to get value of the property defined in that file on JSP page. Is there a way to do that in the way

${something.myProperty}?

推荐答案

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

PropertyPlaceholderConfigurer can only parse placeholders in Spring configuration (XML or annotations). Is very common in Spring applications use a Properties bean. You can access it from your view this way (assuming you are using 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']}.

Then, in your JSP, you can use ${properties.myProperty} or ${properties['my.property']}.

这篇关于如何使用JSP的PropertyPlaceholderConfigurer中指定的属性文件中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆