Java 如何使用 JSP 中 PropertyPlaceholderConfigurer 中指定的属性文件中的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3933862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 07:08:41  来源:igfitidea点击:

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

javaspringspring-3

提问by glaz666

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

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

${something.myProperty}?

采纳答案by sinuhepop

PropertyPlaceholderConfigurercan only parse placeholders in Spring configuration (XML or annotations). Is very common in Spring applications use a Propertiesbean. You can access it from your view this way (assuming you are using InternalResourceViewResolver):

PropertyPlaceholderConfigurer只能解析 Spring 配置(XML 或注释)中的占位符。在 Spring 应用程序中使用Propertiesbean是很常见的。您可以通过这种方式从您的视图访问它(假设您正在使用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']}.

然后,在您的 JSP 中,您可以使用${properties.myProperty}${properties['my.property']}

回答by Matt

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

要使用列表中可能不存在的多个位置,可以使用 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>

回答by anttix

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

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

回答by btpka3

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

在 Spring 3.1 之后,您可以像这样使用<spring:eval />带有SpEL 的标签:

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

回答by user3331318

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:

这将向您显示 schema 表,您至少具有选择权限:

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

回答by Nikhil Kotak

`<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

和。你的JSP 来了

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