如何在Freemarker模板中访问Spring应用程序属性? [英] How to access a Spring application property in a Freemarker template?

查看:82
本文介绍了如何在Freemarker模板中访问Spring应用程序属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have Java webapp that uses Spring 3.1, with Freemarker templates for rendering the view. I want to conditionally display a link in the view, based on the true/false value of a particular application property.

我在src/main/resources/application.properties中定义了以下应用程序属性:

I have the following app property defined in src/main/resources/application.properties:

showLink=true

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

If I were using a regular JSP with Spring MVC, I could use SpEL to conditionally display the link based on the value of showLink:

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

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

How do I do this in a Freemarker template? I tried doing something like this, but couldn't get it to work:

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

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

我查看了

I looked at the Spring freemarker macros (in in spring.ftl), but the closest thing I see is the ability to get a message property, not an app property.

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

  1. I looked at the macros in spring.ftl, but the closest it comes is giving me message properties.

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

Also, I can't inject the value into the controller and then put it into the ModelMap, because my FreeMarker template is the header for all pages so it's auto-imported:

<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. 我找到了博客文章介绍如何将对SpEL的支持手动添加到Freemarker.在这种情况下,我宁愿不做所有这些事情.

  1. I found a blog post describing how to manually add support for SpEL to Freemarker. I'd rather not do all of that for this one case where I need it.

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

Creating a custom tag library to retrieve the application property value, so I could do something like this in my freemarker template:

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

推荐答案

我在Spring中使用

I load the properties in spring using

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

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

And then in the config I use the "freemarkerVariables" attribute, e.g.

<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都是文件中的所有属性.

where var1/var2/value1/value2 are all properties from your file.

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

You can the access the properties in freemarker like this

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

该解决方案的唯一缺点是,Freemarker无法自动使用属性.您需要添加所需的内容.

The only downside with this solution is, properties will not automatically be available to Freemarker. You need to add the ones you want.

这篇关于如何在Freemarker模板中访问Spring应用程序属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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