开发者

Access to freemarker variables in html file using FreeMarkerViewResolver

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>
&开发者_如何学Pythonlt;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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜