1.工程引入依赖
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>
2.创建模板文件
模板文件中四种元素
1、文本,直接输出的部分
2、注释,即<#--...-->格式不会输出
3、插值(Interpolation):即${..}部分,将使用数据模型中的部分替代输出
4、FTL指令:FreeMarker指令,和HTML标记类似,名字前加#予以区分,不会输出。
我们现在就创建一个简单的创建模板文件test.ftl
<html>
<head>
<meta charset="utf-8">
<title>Freemarker入门小DEMO </title>
</head>
<body>
<#--我只是一个注释,我不会有任何输出 -->
${name},你好。${message}<br>
<#assign age="18">
年龄:${age}<br>
<#assign info={"sex":"男","hobbys":"唱跳rap"}>
性别:${info.sex}<br>
爱好:${info.hobbys}
<#include "head.ftl">
<ul>
无序列表的个数:${grandList?size}
<#list grandList as entity>
<li> ${entity} </li> </#list> </ul> <h3>map集合取数据</h3> 姓名:${person.name} 年龄:${person.age}<br> <h3>时间类型:</h3> 当前日期:${date?date}<br> 当前时间:${date?time}<br> 时间戳:${date?datetime}<br> 时间格式化:${date?string("yyyy/MM/dd HH:mm:ss")} <h3>身份证号</h3> 以字符串形式输出:${id?c} <h3>??判断变量是否存在</h3> <#if aaa??> aaa存在 <#else > aaa不存在 </#if> <h3>判断变量是否存在,不存在时!赋默认值</h3> <#if aaa??> aaa存在 ${aaa} <#else > aaa不存在 ${aaa!'111'} </#if> </body> </html>
3.配置文件resources/spring/application-service.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <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> </beans>
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"> <!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-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(); } } }