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

发布于 2024-09-27 19:15:18 字数 231 浏览 2 评论 0原文

在我的应用程序上下文中,我定义了属性文件:

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

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

${something.myProperty}?

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}?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

冷月断魂刀 2024-10-04 19:15:18

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

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

任性一次 2024-10-04 19:15:18

在 Spring 3.1 之后,您可以将 标签与 SpEL 像这样:

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

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

<spring:eval expression="@applicationProps['application.version']" 
             var="applicationVersion"/>
懷念過去 2024-10-04 19:15:18

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

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>
滥情哥ㄟ 2024-10-04 19:15:18

要在视图中使用递归属性占位符扩展,您需要不同的解决方案,请查看以下答案:

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

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

一桥轻雨一伞开 2024-10-04 19:15:18
`<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>`
`<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>`
_蜘蛛 2024-10-04 19:15:18

这将向您显示当前模式(您已登录)的表:

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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文