jsp中使用spring的property属性

stackoverflow里的大神已经给了方案
引用

    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.





第二种方案
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>

猜你喜欢

转载自powertech.iteye.com/blog/2278454