【问题标题】:How can I read system properties in a tagx/jspx?如何读取 tagx/jspx 中的系统属性?
【发布时间】:2012-05-18 17:51:16
【问题描述】:

我正在定义一个名为“version.tagx”的 tagx 文件。这个标签的职责是发出一个锚标签,它的显示文本是应用程序的版本号。目前,该文件的定义如下所示:

<jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:spring="http://www.springframework.org/tags" xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
  <jsp:output omit-xml-declaration="yes" />

  <jsp:directive.attribute name="render" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Indicate if the contents of this tag and all enclosed tags should be rendered (default 'true')" />

  <c:if test="${empty render or render}">
    <spring:message code="global_version" />
    <spring:url var="changelog" value="/resources/changelog.txt" />

    <c:out value=": " />
    <a href="${changelog}" title="Built by ${application_builtBy} on ${application_buildTime}">${application_version}</a>
  </c:if>
</jsp:root>

我的应用程序是在 Tomcat 7x 容器中运行的 Spring MVC 应用程序。我的 applicationContext.xml 中有以下行

<context:property-placeholder location="classpath*:META-INF/spring/*_${spring.profiles.active}.properties,classpath:app-info.properties"/>

我已通过遵循 DEBUG 日志消息确认 app-info.properties 文件是由 Spring 发现的,并且(可能)该文件中的属性值已加载到我的运行时中。

这是日志消息

2012-05-09 23:45:24,237 [main] INFO  org.springframework.context.support.PropertySourcesPlaceholderConfigurer - Loading properties file from class path resource [app-info.properties]
2012-05-09 23:45:24,237 [main] DEBUG org.springframework.core.env.MutablePropertySources - Adding [localProperties] PropertySource with lowest search precedence

这是我的 app-info.properties 文件的内容:

application_version=1.0
application_buildTime=05-04-2012 00:00:00
application_builtBy=me
application_buildNumber=55

我想要的是让我的 tagx 发出

Version: <a href="path/to/changelog.html" title="Built by me on 05-04-2012 00:00:00">1.0</a>

而目前我得到的是:

Version: <a href="path/to/changelog.html" title="Built by  on "></a>

有谁知道实现这一点的方法吗?我应该尝试一种完全不同的方法来完全放弃属性文件吗?

【问题讨论】:

    标签: java spring jsp


    【解决方案1】:

    在 webmvc-config.xml 添加 util:properties 其中 /WEB-INF/spring/application.properties 是属性文件的路径:

    <util:properties id="applicationProps" location="/WEB-INF/spring/application.properties"/>
    

    然后,只需在你的锚标记之前添加这个:

    <spring:eval expression="@applicationProps['application_builtBy']" var="application_builtBy"/>
    <spring:eval expression="@applicationProps['application_buildTime']" var="application_buildTime"/>
    <spring:eval expression="@applicationProps['application_version']" var="application_version"/>
    

    希望对你有帮助。

    【讨论】:

    • 甜蜜!自从我发布这个问题以来,我一直在努力解决这个问题,与此同时,我一直在依赖一个丑陋的工作。真的很感激洞察力!我能问一下你是怎么知道答案的吗?是否有一些我应该收藏的好文档?
    • Web MVC 框架参考手册 (static.springsource.org/spring/docs/3.0.x/…) 在一个很好的示例中使用 applicationProps 来根据 application.version 更改 mvc:resources 路径。当您需要确保在部署新版本时更新 javascript 和其他资源时,这很有用。如果没有此配置,则不会从浏览器的缓存中更新 javascript。
    【解决方案2】:

    您还可以执行的操作不会限制您在单个属性占位符中查找属性,或者如果您使用 java 配置并且仅实例化 PropertySourcesPlaceholderConfigurer 是使用环境对象:

    <spring:eval expression="@environment.getProperty('application_builtBy')" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-25
      • 2018-09-30
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      相关资源
      最近更新 更多