FreeMarker的语法
- 取map集合中的值${key}
- 取pojo类的值${student.sname}
- 循环<#list 集合的key as 单一变量> 取下标 单一变量_index
- 判断 <#if><#else>
java操作FreeMarker
Configuration configuration=new Configuration(Configuration.getVersion());
configuration.setDirectoryForTemplateLoading(new File(""));
configuration.setDefaultEncoding("utf-8");
Template template=configuration.getTemplate("test.ftl");
Writer out=new FileWriter(new File("D:/test/test.txt"));
Map dataModel=new HashMap<>();
dataModel.put("hello", "hello freemarker");
template.process(dataModel, out);
Spring整合FreeMarker
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
<property name="defaultEncoding" value="UTF-8" />
</bean>