首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何读取tagx/jspx中的系统属性?

如何读取tagx/jspx中的系统属性?
EN

Stack Overflow用户
提问于 2012-05-10 15:23:44
回答 2查看 5.4K关注 0票数 1

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

代码语言:javascript
复制
<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>

我的应用程序是在Tomcat7x容器中运行的Spring MVC应用程序。我的applicationContext.xml中有下面这行

代码语言:javascript
复制
<context:property-placeholder location="classpath*:META-INF/spring/*_${spring.profiles.active}.properties,classpath:app-info.properties"/>

通过跟踪调试日志消息,我已经确认了Spring发现了app-info.properties文件,并且(假设)该文件中的属性值已经加载到我的运行时中。

以下是日志消息

代码语言:javascript
复制
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文件的内容:

代码语言:javascript
复制
application_version=1.0
application_buildTime=05-04-2012 00:00:00
application_builtBy=me
application_buildNumber=55

我要的是让我的tagx发出

代码语言:javascript
复制
Version: <a href="path/to/changelog.html" title="Built by me on 05-04-2012 00:00:00">1.0</a>

目前我得到的是:

代码语言:javascript
复制
Version: <a href="path/to/changelog.html" title="Built by  on "></a>

有没有人知道有什么方法可以做到这一点?我是否应该尝试一种完全不同的方法,放弃所有的属性文件?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-28 07:47:12

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

代码语言:javascript
复制
<util:properties id="applicationProps" location="/WEB-INF/spring/application.properties"/>

然后,只需在您的锚点标记前添加以下内容:

代码语言:javascript
复制
<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"/>

希望能有所帮助。

票数 4
EN

Stack Overflow用户

发布于 2015-01-20 22:46:11

你还可以做的并不局限于在单个属性占位符中查找属性,或者如果你正在使用java配置并且仅仅实例化一个环境就是使用PropertySourcesPlaceholderConfigurer对象:

代码语言:javascript
复制
<spring:eval expression="@environment.getProperty('application_builtBy')" />
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10529229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档