1.什么是freemarker
FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出。FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java 等。
目前企业中:主要用Freemarker做静态页面或是页面展示.。
2.基本语句
2.1基本类型
String var1="hello";
int var2=12;
boolean var3=true;
取值:
${var1}
${var2}
${var2+20}
${var3?string("yes","no")}
显示:
hello
12
32
yes
注意:第三种的boolean值只能这样写
2.2封装类型
map.put("user",new User(1,"小明"));
取值:
${user.id}
${user.name}
2.3日期
map.put("date",new Date());
${date?string('yyyy-MM-dd')}
2.4设置默认值
integer a=null;
${a!"a为空时显示"}
显示:
a为空时显示
2.5转义HTML
map.put("m","<h1>标题<h1>");
${m?html}
2.6遍历集合
User user1=new User(1,"a");
User user2=new User(1,"a");
list.add(user1);
list.add(user2);
map.put("list",list);
<#list list as item>
${item.name}<br>
${item.name}
</#list>
2.7条件判断
<#if item_index % 2 == 0>
<tr bgcolor="red">
<#else>
<tr bgcolor="green">
</#if>
判断val的值是否为null:<br>
<#if val??>
val中有内容
<#else>
val的值为null
</#if>
引用模板测试:<br>
<#include "hello.ftl">
2.8其他内置函数等
3.语法实践
建立一个student的bean类,包含id,name,age,address属性
@Test
public void testFreeMarker() throws Exception {
// 1、创建一个模板文件
// 2、创建一个Configuration对象
Configuration configuration = new Configuration(Configuration.getVersion());
// 3、设置模板文件保存的目录
configuration.setDirectoryForTemplateLoading(
new File("D:/eclipseWeb/e3-item-web/src/main/webapp/WEB-INF/ftl"));
// 4、模板文件的编码格式,一般就是utf-8
configuration.setDefaultEncoding("utf-8");
// 5、加载一个模板文件,创建一个模板对象。
Template template = configuration.getTemplate("student.ftl");
// 6、创建一个数据集。可以是pojo也可以是map。推荐使用map
Map data = new HashMap<>();
data.put("hello", "hello freemarker!");
// 创建一个pojo对象
Student student = new Student(1, "小明", 18, "回龙观");
data.put("student", student);
// 添加一个list
List<Student> stuList = new ArrayList<>();
stuList.add(new Student(1, "小明1", 18, "回龙观"));
stuList.add(new Student(2, "小明2", 19, "回龙观"));
stuList.add(new Student(3, "小明3", 20, "回龙观"));
stuList.add(new Student(4, "小明4", 21, "回龙观"));
stuList.add(new Student(5, "小明5", 22, "回龙观"));
stuList.add(new Student(6, "小明6", 23, "回龙观"));
stuList.add(new Student(7, "小明7", 24, "回龙观"));
stuList.add(new Student(8, "小明8", 25, "回龙观"));
stuList.add(new Student(9, "小明9", 26, "回龙观"));
data.put("stuList", stuList);
// 添加日期类型
data.put("date", new Date());
// null值的测试
data.put("val", "123");
// 7、创建一个Writer对象,指定输出文件的路径及文件名。
Writer out = new FileWriter(new File("D:/ProjectResources/ssm/stduent.html"));
// 8、生成静态页面
template.process(data, out);
// 9、关闭流
out.close();
}
测试集合的遍历以及日期,对象的输出
<html>
<head>
<title>student</title>
</head>
<body>
学生信息:<br>
学号:${student.id}
姓名:${student.name}
年龄:${student.age}
家庭住址:${student.address}<br>
学生列表:
<table border="1">
<tr>
<th>序号</th>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>家庭住址</th>
</tr>
<#list stuList as stu>
<#if stu_index % 2 == 0>
<tr bgcolor="red">
<#else>
<tr bgcolor="green">
</#if>
<td>${stu_index}</td>
<td>${stu.id}</td>
<td>${stu.name}</td>
<td>${stu.age}</td>
<td>${stu.address}</td>
</tr>
</#list>
</table>
<br>
<!-- 可以使用?date,?time,?datetime,?string(parten)-->
当前日期:${date?string("yyyy/MM/dd HH:mm:ss")}<br>
<!-- 可以使用value!后加默认值-->
null值的处理:${val!"val的值为null"}<br>
判断val的值是否为null:<br>
<#if val??>
val中有内容
<#else>
val的值为null
</#if>
引用模板测试:<br>
<#include "hello.ftl">
</body>
</html>
4.整合springmvc
需要spring-context-support,在配置xml
<!-- 配置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>
编写测试
@RequestMapping("/genhtml")
@ResponseBody
public String genHtml() throws Exception {
Configuration configuration = freeMarkerConfigurer.getConfiguration();
//加载模板对象
Template template = configuration.getTemplate("hello.ftl");
//创建一个数据集
Map data = new HashMap<>();
data.put("hello", 123456);
//指定文件输出的路径及文件名
Writer out = new FileWriter(new File("D:/ProjectResources/ssm/hell2.html"));
//输出文件
template.process(data, out);
//关闭流
out.close();
return "OK";
}