一:freemarker是什么?html
freemarker是一个模板引擎,基于定义的模板和数据生成对应的文本(HTML,xml,java等),是一个生成文本的工具。java
二:freemarker的使用方法web
(1)在工程中引入freemarker相关的依赖spring
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency>
(2)使用的步骤app
第一步:建立一个Configuration对象,直接new一个便可,构造参数是freemarker的版本号webapp
第二步:设置模板文件所在的路径,须要给出在磁盘上储存的全路径ide
第三步:设置生成的文件的编码格式,通常为utf-8格式函数
第四步:加载模板,建立模板对象工具
第五步:建立模板使用的数据集,可使pojo也能够是map类型的测试
第六步:建立Write流对象,将文件文件输出,须要指定生成的文件的名称
第七步:调用模板的process方法,生成相应的文本
第八步:关闭流
@Test public void genFile() throws Exception { // 第一步:建立一个Configuration对象,直接new一个对象。构造方法的参数就是freemarker对于的版本号。 Configuration configuration = new Configuration(Configuration.getVersion()); // 第二步:设置模板文件所在的路径。 configuration.setDirectoryForTemplateLoading(new File("D:/workspace/item-web/src/main/webapp/WEB-INF/ftl")); // 第三步:设置模板文件使用的字符集。通常就是utf-8. configuration.setDefaultEncoding("utf-8"); // 第四步:加载一个模板,建立一个模板对象。 Template template = configuration.getTemplate("hello.ftl"); // 第五步:建立一个模板使用的数据集,能够是pojo也能够是map。通常是Map。 Map dataModel = new HashMap<>(); //向数据集中添加数据 dataModel.put("hello", "this is my first freemarker test."); // 第六步:建立一个Writer对象,通常建立一FileWriter对象,指定生成的文件名。 Writer out = new FileWriter(new File("D:/temp/out/hello.html")); // 第七步:调用模板对象的process方法输出文件。 template.process(dataModel, out); // 第八步:关闭流。 out.close(); }
(3)模板的语法
1.访问map中的key
${key}便可得到对应的value值
2.访问pojo中的属性
以student对象为例: ${student.id} ${student.name}便可取得student对象中的id值和name值
3.去集合中的元素
例如:遍历学生对象集合,取出每个学生的id值和name值
<#list studentList as student>
${student,id} ${student.name}
<#list>
4.取集合中的下标
<#list studentList as student>
对象+下划线+index 便可得到下标值
${student_index}
<#list>
5.判断
<#list sutdnetList as student>
进行奇偶数的判断
<#if student_index % 2 ==0>
//偶数的处理过程
<#else>
//奇数的处理过程
<#if>
<#list>
6.日期类型的格式化
${date?date} 当前日期
${date?time} 当前时间
${date?datetime} 当前日期和时间
${date?string("yyyy-MM-dd hh:mm:ss")} 设置日期格式
7.NULL的处理
!对输出的控制处理,只输出,无返回值
${name} 若是name为空就会报错
${name!}若是name为空,不会报错,没有输出
${name!"默认值'} 若是name为空,就会输出默认值
${name!666}若是name为空,会输出666
${student.name}若是student或者name为空,报错
${student.name!"默认值"}若是student为空,会报错,name为空,输出默认值
??测试是否为null,返回Boolean类型的值
product.color??将只测试color是否为null
(product.color)??将测试product和color是否存在null
??和?的区别
??是判断对象是否为空,例如:<#if object??>object对象不为空(即object存在)</#if>
?后面要加内建函数名,例如:<#if object?exists>object对象不为空(即object存在)</#if>
<#if str??>${str?string}</#if><#--将str以字符串形式显示-->
8.include标签
<#include “模板名称“>
三:freemarker和spring的整合
(1)将Configuration对象的建立交给spring统一管理,为该对象注入两个属性,模板路径和文件的编码格式
<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>
(2)进行测试
@Controller public class HtmlGenController { @Autowired private FreeMarkerConfigurer freeMarkerConfigurer; @RequestMapping("/genhtml") @ResponseBody public String genHtml()throws Exception { // 一、从spring容器中得到FreeMarkerConfigurer对象。 // 二、从FreeMarkerConfigurer对象中得到Configuration对象。 Configuration configuration = freeMarkerConfigurer.getConfiguration(); // 三、使用Configuration对象得到Template对象。 Template template = configuration.getTemplate("hello.ftl"); // 四、建立数据集 Map dataModel = new HashMap<>(); dataModel.put("hello", "1000"); // 五、建立输出文件的Writer对象。 Writer out = new FileWriter(new File("D:/temp/term197/out/spring-freemarker.html")); // 六、调用模板对象的process方法,生成文件。 template.process(dataModel, out); // 七、关闭流。 out.close(); return "OK"; } }
错误的地方但愿你们指正。