如何读取tagx/jspx中的系统属性? [英] How can I read system properties in a tagx/jspx?

查看:91
本文介绍了如何读取tagx/jspx中的系统属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am defining a tagx file called "version.tagx". The responsibility of this tag is to emit an anchor tag whose display text is the version number of the application. Currently, the definition of the file looks like this:

<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中有以下行

My application is a Spring MVC application running in a Tomcat 7x container. I have the following line in my applicationContext.xml

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

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

I have confirmed through following the DEBUG log message the app-info.properties file is discovered by Spring and (presumably) the property values within that file have been loaded into my runtime.

这是日志消息

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文件的内容:

And here are the contents of my app-info.properties file:

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

我想要的是让我的标签发出

What I want is for my tagx to emit

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>

有人知道实现此目标的方法吗?我是否应该尝试一种完全不同的方法来完全放弃所有属性文件?

Does anyone know of a way to accomplish this? Should I be trying a completely different approach that forgoes properties files all togher?

推荐答案

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

In webmvc-config.xml add util:properties where /WEB-INF/spring/application.properties is the path to the properties file:

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

然后,只需将其添加到锚标记之前即可

Then, simply add this before your anchor tag:

<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"/>

希望有帮助.

这篇关于如何读取tagx/jspx中的系统属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆