简体   繁体   中英

How to use property from property file specified in PropertyPlaceholderConfigurer in JSP

In my application context I have defined properties file:

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

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 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']} .

After Spring 3.1, you can use <spring:eval /> tag with SpEL like this:

<spring:eval expression="@applicationProps['application.version']" 
             var="applicationVersion"/>

To use recursive property placeholder expansion in views, you need a different solution, take a look at this answer:

https://stackoverflow.com/a/10200249/770303

`<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" 
  id="messageSource"
  p:basenames="WEB-INF/i18n/site"
  p:fallbackToSystemLocale="false"/>`

Now this is your Properties File

`site.name=Cool Bananas`

And. Here goes your JSP

`<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<html>
  <head>
    <title><spring:message code="site.name"/></title>
  </head>
  <body>
  </body>
</html>`

To use with multiple locations in a list which might not be present as can be done with the context:property-placeholder bean:

<beans:bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <beans:property name="ignoreResourceNotFound" value="true" />
    <beans:property name="locations">
        <beans:list>
            <beans:value>classpath:application.properties</beans:value>
            <beans:value>classpath:environment.properties</beans:value>
            <beans:value>classpath:environment-${env}.properties</beans:value>
        </beans:list>
    </beans:property>
</beans:bean>

This will show you the tables of the current schema (which you are logged in):

select table_name from user_tables order by table_name;

This will show you the tables of schema , for which you have select rights at least:

select owner, table_name from all_tables where owner='<owner>' order by owner, table_name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM