Доступ к переменным freemarker в html-файле с помощью FreeMarkerViewResolver - PullRequest
2 голосов
/ 19 августа 2011

Я настроил свое веб-приложение, как эта ссылка 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>

Пример моего 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>

Я могу получить доступ к $ {isserId}, потому что у меня есть request.setConversationScope ("isserId", "1234"), но я хочу также получить доступ к переменной foo freemarker, но когда я использую $ {foo}, я получил выражение foo не определено. Есть идеи?

1 Ответ

1 голос
/ 22 августа 2011

Благодаря @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}.

Еще раз спасибо.Это сделано сейчас.

...