使用 FreeMarkerViewResolver 访问 html 文件中的 freemarker 变量
我像这个链接配置了我的网络应用程序 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢@ddekany,它成功了,我的配置没有任何问题。我之前很困惑。 :)。是的,foo 可以简单地显示为 ${foo}。
另请注意,如果我想在应用程序运行期间添加新变量,我可以使用
${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
and i can access with ${foo2}.
Thanks again. It's done now.