2012-05-10 27 views
1

我正在定义一个名为“version.tagx”的标签文件。该标签的职责是发出一个锚标签,其显示文本是应用程序的版本号。目前,文件的定义如下所示:如何读取tagx/jspx中的系统属性?

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

我已经通过以下的app-info.properties文件是由春,发现了DEBUG日志信息确认(大概)该文件中的属性值已加载进入我的运行时。

这里是日志消息

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> 

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

回答

4

在webmvc-config.xml中添加UTIL:性能,其中/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"/> 

希望它有帮助。

+0

甜!自从我发布这个问题以来,我一直在努力解决这个问题,并且在此期间一直依靠丑陋的工作。真心感谢洞察!我可以问你如何知道答案?是否有一些好的文档可以保存为书签? –

+0

Web MVC框架参考手册(http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html)使用applicationProps作为改变mvc:资源路径的良好示例在application.version上。当您需要确保在部署新版本时更新javascript和其他资源时,这非常有用。没有这个配置,javascript不会从浏览器的缓存中更新。 – jalcalav

1

什么,你也可以做到这一点不配合你在一个单一的财产占位符查找性能,或者如果您使用的是Java的配置,只是实例化一个PropertySourcesPlaceholderConfigurer是使用环境对象:

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