package org.xxx.app.web;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import freemarker.core.Environment;
import freemarker.template.ObjectWrapper;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
public class FreemarkerGobleDataModels implements TemplateDirectiveModel {
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body) throws TemplateException, IOException {
Map<String, Object> gobleMap = new HashMap<String, Object>();
gobleMap.put("hello1", "hello 1");
gobleMap.put("hello2", "hello 2");
gobleMap.put("hello3", "hello 3");
gobleMap.put("hello4", "hello 4");
gobleMap.put("hello5", "hello 5");
env.setVariable("gobleMap",
ObjectWrapper.DEFAULT_WRAPPER.wrap(gobleMap));
if (body != null) {
body.render(env.getOut());
}
}
}
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="freemarkerSettings" ref="freemarkerConfiguration" />
<property name="templateLoaderPath">
<value>/WEB-INF/ftl/</value>
</property>
<property name="freemarkerVariables">
<map>
<entry key="xml_escape" value-ref="fmXmlEscape" />
<entry key="mygoble" value-ref="mygoble"/>
</map>
</property>
</bean>
<bean id="mygoble" class="org.xxx.app.web.FreemarkerGobleDataModels" />
<bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />
<@mygoble />
${gobleMap.hello1}<br/>
${gobleMap.hello2}<br/>
${gobleMap.hello3}<br/>
${gobleMap.hello4}<br/>
${gobleMap.hello5}<br/>