【发布时间】:2011-04-25 10:25:04
【问题描述】:
在我的应用程序上下文中,我定义了属性文件:
<context:property-placeholder location="classpath:application.properties" />
我想获取 JSP 页面上该文件中定义的属性的值。有没有办法做到这一点
${something.myProperty}?
【问题讨论】:
在我的应用程序上下文中,我定义了属性文件:
<context:property-placeholder location="classpath:application.properties" />
我想获取 JSP 页面上该文件中定义的属性的值。有没有办法做到这一点
${something.myProperty}?
【问题讨论】:
PropertyPlaceholderConfigurer 只能解析 Spring 配置中的占位符(XML 或注解)。在 Spring 应用程序中很常见使用 Properties bean。您可以通过这种方式从您的视图中访问它(假设您使用的是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']}。
【讨论】:
在 Spring 3.1 之后,你可以像这样使用<spring:eval /> 标签和SpEL:
<spring:eval expression="@applicationProps['application.version']"
var="applicationVersion"/>
【讨论】:
与列表中可能不存在的多个位置一起使用,可以使用 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>
【讨论】:
要在视图中使用递归属性占位符扩展,您需要一个不同的解决方案,看看这个答案:
【讨论】:
`<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
id="messageSource"
p:basenames="WEB-INF/i18n/site"
p:fallbackToSystemLocale="false"/>`
现在这是您的属性文件
`site.name=Cool Bananas`
而且。 这是您的 JSP
`<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<html>
<head>
<title><spring:message code="site.name"/></title>
</head>
<body>
</body>
</html>`
【讨论】:
这将向您显示当前架构的表(您已登录):
select table_name from user_tables order by table_name;
这将显示您至少拥有选择权限的 schema 表:
select owner, table_name from all_tables where owner='<owner>' order by owner, table_name;
【讨论】: