使用 FreeMarkerViewResolver 访问 html 文件中的 freemarker 变量

发布于 2024-11-30 04:29:12 字数 2426 浏览 1 评论 0原文

我像这个链接配置了我的网络应用程序 http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch17s04.html

我的上下文

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
    <property name="freemarkerSettings">
        <props>
            <prop key="tag_syntax">square_bracket</prop>
            <prop key="auto_import">spring.ftl as spring, echannels.ftl as echannels
            </prop>
            <prop key="template_update_delay">2147483647</prop>
        </props>
    </property>
</bean>

<bean id="htmlViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="contentType" value="text/html;charset=ISO-8859-1" />
    <property name="cache" value="${viewResolver.html.cache}" />
    <property name="prefix" value="html/" />
    <property name="suffix" value=".html" />
</bean>

我使用 spring-webflow 链接我的所有页面html。 在我的页面 html 中,我可以使用 ${name_variable} 访问转换范围变量。 我的问题是:如果我在上下文中定义了 FreeMarker 变量“foo”,我如何在 html 中访问它:

<bean id="freemarkerConfig"
    class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
    <property name="freemarkerVariables">
        <map>
            <entry key="foo" value="foo" />
        </map>
    </property>
</bean>

Example of my HTML

<html>
<head>
    <title>Welcome</title>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>

<body>
    <h1>Welcome ${issuerId}</h1>

    <form action="?execution=${flowExecutionKey}" method="post" name="defineFeeForm">
        <input type="submit" name="_eventId_next" value="Next" /> <br/>
    </form>
</body>

我可以访问 ${issuerId} 因为我有 request.setConversationScope("issuerId", "1234") 但我也想访问 foo freemarker 变量,但是当我使用 ${foo} 时,我得到 Expression foo is undefined 。 有什么想法吗?

I configured my web application like this link http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch17s04.html

My context

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
    <property name="freemarkerSettings">
        <props>
            <prop key="tag_syntax">square_bracket</prop>
            <prop key="auto_import">spring.ftl as spring, echannels.ftl as echannels
            </prop>
            <prop key="template_update_delay">2147483647</prop>
        </props>
    </property>
</bean>

<bean id="htmlViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="contentType" value="text/html;charset=ISO-8859-1" />
    <property name="cache" value="${viewResolver.html.cache}" />
    <property name="prefix" value="html/" />
    <property name="suffix" value=".html" />
</bean>

I use spring-webflow to link all my page html.
In my page html, i can access to conversion scope variables by using ${name_variable}.
My question is: how could i access to FreeMarker variable "foo" in my html if i defined it in my context:

<bean id="freemarkerConfig"
    class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
    <property name="freemarkerVariables">
        <map>
            <entry key="foo" value="foo" />
        </map>
    </property>
</bean>

Example of my HTML

<html>
<head>
    <title>Welcome</title>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>

<body>
    <h1>Welcome ${issuerId}</h1>

    <form action="?execution=${flowExecutionKey}" method="post" name="defineFeeForm">
        <input type="submit" name="_eventId_next" value="Next" /> <br/>
    </form>
</body>

I can access to ${issuerId} because i have request.setConversationScope("issuerId", "1234") but i want to access also to foo freemarker variable but when i use ${foo}, i got Expression foo is undefined.
Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘心安 2024-12-07 04:29:12

感谢@ddekany,它成功了,我的配置没有任何问题。我之前很困惑。 :)。是的,foo 可以简单地显示为 ${foo}。

另请注意,如果我想在应用程序运行期间添加新变量,我可以使用

Map<String, Object> variables = new HashMap<String, Object>();
variables.put("foo2", "foo2");
freeMarkerConfigurer.getConfiguration().setAllSharedVariables(new SimpleHash(variables,  freeMarkerConfigurer.getConfiguration().getObjectWrapper()));

${foo2} 进行访问。

再次感谢。现在已经完成了。

Thanks to @ddekany , it's worked, there're nothing wrong in my configuration. I confused before. :). Yes, foo is visible simply as ${foo}.

Another remark, if i want to add new variables during running of my application, i can use

Map<String, Object> variables = new HashMap<String, Object>();
variables.put("foo2", "foo2");
freeMarkerConfigurer.getConfiguration().setAllSharedVariables(new SimpleHash(variables,  freeMarkerConfigurer.getConfiguration().getObjectWrapper()));

and i can access with ${foo2}.

Thanks again. It's done now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文