如何在Freemarker模板中访问Spring应用程序属性?

时间:2013-03-08 20:31:40

标签: spring spring-mvc freemarker

我有使用Spring 3.1的Java webapp,Freemarker模板用于渲染视图。我想根据特定应用程序属性的true / false值有条件地在视图中显示一个链接。

我在src/main/resources/application.properties中定义了以下app属性:

showLink=true

如果我使用常规JSP和Spring MVC,我可以使用SpEL根据showLink的值有条件地显示链接:

<c:if test="${configuration['showLink']}">
    <a href="...">some link</a>
</c:if>

如何在Freemarker模板中执行此操作?我尝试过做这样的事情,但无法让它发挥作用:

<#assign showLink>${configuration['showLink']}</#assign>

<#if showHelpLink>
    <a href="...">some link</a>
</#if>

我查看了Spring freemarker macros(在spring.ftl中),但我看到的最接近的是能够获取消息属性,而不是app属性。

我尝试过的东西不起作用

  1. 我查看了spring.ftl中的宏,但它最接近的是给我消息属性。

  2. 此外,我无法将值注入控制器,然后将其放入ModelMap,因为我的FreeMarker模板是所有页面的标题,因此它是自动导入的:

  3. <bean id="abstractFreemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" abstract="true">
        ...
        <property name="freemarkerSettings">
            <props>
                <prop key="auto_import">
                    /spring.ftl as spring, /myTemplate.ftl as myTemplate
                </prop>
            </props>
        </property>
        ...
    </bean>
    

    我尚未尝试过的事情

    1. 我找到了一个blog post,描述了如何手动将支持SpEL添加到Freemarker。在我需要它的情况下,我宁愿不做所有这些。

    2. 创建自定义标记库以检索应用程序属性值,因此我可以在我的freemarker模板中执行以下操作:

    3. <#assign showLink><foo:getAppProperty name="showLink"/></#assign>
      

3 个答案:

答案 0 :(得分:5)

我使用

在spring中加载属性
<util:properties id="myProperties" location="classpath:/myprops.properties" />

然后在配置中我使用“freemarkerVariables”属性,例如

<bean id="abstractFreemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" abstract="true">
...
    <property name="freemarkerVariables" ref="staticAttributesMap" />

<util:map id="staticAttributesMap">
    <entry key="var1" value="${var1}" />
    <entry key="var2" value="${var2}" />

    <entry key="myMap">
        <map>
            <entry key="v1" value="${value1}" />
            <entry key="v2" value="${value2}" />
        </map>
    </entry>
</util:map>

其中var1 / var2 / value1 / value2是文件中的所有属性。

您可以像这样访问freemarker中的属性

$var1$
$var2$
$myMap.v1$
$myMap.v2$

此解决方案的唯一缺点是,Freemarker不会自动提供属性。你需要添加你想要的那些。

答案 1 :(得分:1)

由于某些未知原因,接受的解决方案对我不起作用。我正在使用bean加载属性。所以假设存在一个用这个bean映射的Java类:

<bean id="appProperties" class="com.myCompany.AppProperties">
    <property name="appVersion" value="${app.version}" />
</bean>

我使用这个xml映射在Freemarker中访问bean变量:

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/views/"/>
    <property name="defaultEncoding" value="UTF-8" />
    <property name="freemarkerSettings">
        <props>
            <prop key="auto_import">spring.ftl as spring</prop>
            <prop key="number_format">0.####</prop>
        </props>
    </property>
    <property name="freemarkerVariables" ref="staticAttributesMap"/>
</bean>

<util:map id="staticAttributesMap">
    <entry key="appversion" value="#{appProperties.appVersion}" />
</util:map>

并且不要忘记在xml文件的顶部添加相应的命名空间(...是您已经拥有的命名空间,以防您直到现在才使用util,就像我一样):

<beans ...
   xmlns:util="http://www.springframework.org/schema/util"
   xsi:schemaLocation="...
     http://www.springframework.org/schema/util
     http://www.springframework.org/schema/util/spring-util-3.0.xsd">

答案 2 :(得分:0)

我无法让已接受的解决方案正常工作,但如果我使用 context:property-placeholder 而不是util:properties加载属性文件,那么一切都无缝地工作:

替换

<util:properties id="myProperties" location="classpath:/myprops.properties" />

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

我实际上是想从maven中获取版本,所以我的位置是

 META-INF/maven/group.id/artifactid/pom.properties

此外,我发现我必须在freemarker中访问变量,如下所示: $ {var1},$ {var2}