1.工程引入依赖
org.freemarker freemarker 2.3.23 junit junit 4.12
2.创建模板文件
模板文件中四种元素
1、文本,直接输出的部分
2、注释,即<#--...-->格式不会输出
3、插值(Interpolation):即${..}部分,将使用数据模型中的部分替代输出
4、FTL指令:FreeMarker指令,和HTML标记类似,名字前加#予以区分,不会输出。
我们现在就创建一个简单的创建模板文件test.ftl
Freemarker入门小DEMO <#--我只是一个注释,我不会有任何输出 --> ${name},你好。${message}
<#assign age="18"> 年龄:${age}
<#assign info={"sex":"男","hobbys":"唱跳rap"}> 性别:${info.sex}
爱好:${info.hobbys} <#include "head.ftl">无序列表的个数:${grandList?size} <#list grandList as entity>
- ${entity}
#list>map集合取数据
姓名:${person.name} 年龄:${person.age}
时间类型:
当前日期:${date?date}
当前时间:${date?time}
时间戳:${date?datetime}
时间格式化:${date?string("yyyy/MM/dd HH:mm:ss")}身份证号
以字符串形式输出:${id?c}??判断变量是否存在
<#if aaa??> aaa存在 <#else > aaa不存在 #if>判断变量是否存在,不存在时!赋默认值
<#if aaa??> aaa存在 ${aaa} <#else > aaa不存在 ${aaa!'111'} #if>
3.配置文件resources/spring/application-service.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
4.web.xml中加载配置文件
xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <context-param> <param-name>contextConfigLocationparam-name> <param-value>classpath*:spring/applicationContext*.xmlparam-value> context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class> listener> web-app>
4.生成文件
使用步骤:
第一步:创建一个 Configuration 对象,freeMarkerConfig.getConfiguration()
第四步:加载一个模板,创建一个模板对象。
第五步:创建一个模板使用的数据集,可以是 pojo 也可以是 map。一般是 Map。
第六步:创建一个 Writer 对象,一般创建一 FileWriter 对象,指定生成的文件名。
第七步:调用模板对象的 process 方法输出文件。
第八步:关闭流
代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext_solr.xml")
public class FreeMarkerTest {
@Autowired
private FreeMarkerConfig freeMarkerConfig;
@Test
public void test01() throws IOException, TemplateException {
//1.创建配置类
Configuration configuration = freeMarkerConfig.getConfiguration();
//4.加载模板
Template template = configuration.getTemplate("test.ftl"); //5.创建数据模型 Map map = new HashMap(); map.put("name", "luli"); map.put("message", "欢迎来到王者荣耀"); //list类型 List list = new ArrayList<>(); list.add("a"); list.add("b"); list.add("c"); list.add("d"); map.put("grandList", list); //map类型 HashMap map1 = new HashMap(); map1.put("name", "曜"); map1.put("age", "20"); map.put("person", map1); //date类型 map.put("date", new Date()); //number类型处理 map.put("id", 41038111112412L);//6.创建输出流 FileWriter fw = new FileWriter("E:\\test.html"); //7.输出 template.process(map, fw); //8.关流 if(fw != null){ fw.close(); } } }