jsp中使用spring的property属性

[url="http://stackoverflow.com/questions/3920088/jsp-servlet-read-parameters-from-properties-file"]stackoverflow里的大神已经给了方案[/url]
[quote]
You can load the properties using java.util.Properties (or commons-configuration) in a ServletContextListener's contextInitialized(..) method.

register the listener with <listener> in web.xml

You then store the Properties into the ServletContext (you can get it from the event) (ctx.setAttribute("properties", properties)

then access the properties using ${applicationScope.properties.propName} (as BalusC noted, applicationScope is optional)

Update:

Initially I thought spring had some ready-to-use facility for that, but it turns out it's not exactly the case. You have two options:

this article explains something similar to my suggestion above, but using spring's PropertyPlaceholderConfigurer

this answer and this answer allow you to expose all your beans, including a PropertyPlaceholderConfigurer to the servlet context.


[/quote]


[url="http://stackoverflow.com/questions/3933862/how-to-use-property-from-property-file-specified-in-propertyplaceholderconfigurer"]第二种方案[/url]

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>

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



引入springUtils
xmlns:util="http://www.springframework.org/schema/util"
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.1.xsd
配置导出的值
<!--页面上使用的属性值 -->
<util:properties id="pageProp">
<prop key="imgUrl">${static_resource_prefix}</prop>
</util:properties>

springmvc-servlet.xml里配置
<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>pageProp</value></list>
</property>
</bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值