Freemarker
简介
- FreeMarker 是一款 主流的模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。
- 模板引擎的目标是数据+模板=结果
- 模板编写为FreeMarker Template Language (FTL)。它是简单的,专用的语言,
安装使用
使用步骤:
-
加载模板
- 创建核心配置对象
- 设置加载目录
- 获取模板对象
-
创建数据
-
产生输出
FreemarkerDemo1.java
//创建核心配置对象
Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
//设置加载目录
configuration.setClassForTemplateLoading(FreemarkerDemo1.class,"");
//得到模板对象
Template t = configuration.getTemplate("demo2.ftl");
//创建数据
Map<String,Object> data = new HashMap<String,Object>();
List<Student> students=new ArrayList<>();
students.add(new Student("1001","Jack",18));
students.add(new Student("1002","Lucy",17));
students.add(new Student("1003","Tom",19));
students.add(new Student("1004","Tim",20));
students.add(new Student("1005","Marry",18));
data.put("students",students);
//产生输出
t.process(data, new OutputStreamWriter(System.out));
FIL取值
语法:
${属性} ${name}
${student.name}
${属性 ! 默认值} ${name ! "不存在该属性"}
${属性 ?String()} ${salary ? String("0.00")}
list迭代列表
Student.java
package entity;
public class Student {
private String sno;
private String name;
private Integer age;
public Student(String sno, String name, Integer age) {
this.sno = sno;
this.name = name;
this.age = age;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
FreemarkerDemo2.java
package freemarker;
import entity.Student;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.*;
public class FreemarkerDemo2 {
public static void main(String[] args) throws IOException, TemplateException {
//创建核心配置对象
Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
//设置加载目录
configuration.setClassForTemplateLoading(FreemarkerDemo2.class,"");
//得到模板对象
Template t = configuration.getTemplate("demo2.ftl");
//创建数据
Map<String,Object> data = new HashMap<String,Object>();
List<Student> students=new ArrayList<>();
students.add(new Student("1001","Jack",18));
students.add(new Student("1002","Lucy",17));
students.add(new Student("1003","Tom",19));
students.add(new Student("1004","Tim",20));
students.add(new Student("1005","Marry",18));
data.put("students",students);
//产生输出
t.process(data, new OutputStreamWriter(System.out));
}
}
demo2.ftl
<#list students as s>
序号:${s_index+1}
学号:${s.sno}
姓名:${s.name}
年龄:${s.age}
-------------------------------
</#list>
list迭代Map
FreemarkerDemo3.java
package freemarker;
import entity.Student;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.*;
public class FreemarkerDemo3 {
public static void main(String[] args) throws IOException, TemplateException {
//创建核心配置对象
Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
//设置加载目录
configuration.setClassForTemplateLoading(FreemarkerDemo3.class,"");
//得到模板对象
Template t = configuration.getTemplate("demo3.ftl");
//创建数据
Map<String,Object> data = new HashMap<String,Object>();
List<Student> students=new ArrayList<>();
students.add(new Student("1001","Jack",18));
students.add(new Student("1002","Lucy",17));
students.add(new Student("1003","Tom",19));
students.add(new Student("1004","Tim",20));
students.add(new Student("1005","Marry",18));
data.put("students",students);
Map studentMap=new LinkedHashMap();
for(Student s:students){
studentMap.put(s.getSno(),s);
}
data.put("studentMap",studentMap);
//产生输出
t.process(data, new OutputStreamWriter(System.out));
}
}
Demo03.ftl
<#list studentMap?keys as k>
${k}-${studentMap[k].name}
</#list>
内建函数
参考手册:http://freemarker.foofun.cn/
Freemarker与Servlet整合
- 在WEB-INF目录下的lib文件夹中导入freemarker包
- 配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>freemarker</servlet-name>
<servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
<!--设置加载目录-->
<init-param>
<param-name>TemplatePath</param-name>
<param-value>/WEB-INF/ftl</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>freemarker</servlet-name>
<!--映射路径--><!--所有扩展名为ftl文件被freemarker加载-->
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>
</web-app>
- 编写Servlet类
package Freemarker;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet("/fre_servlet")
public class FreemarkerServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("----------------------------");
List<Map> students= new ArrayList<>();
for (int i = 0; i < 5; i++) {
Map<String, Object> student = new HashMap<>();
student.put("sno", "S" + i);
student.put("name", "Jack" + i);
students.add(student);
}
request.setAttribute("students", students);
request.getRequestDispatcher("student.ftl").forward(request, response);
}
}
- 编写ftl文件
<#list students as student>
学号:${student.sno}-----姓名:${student.name}<br>
</#list>
- 启动Tomcat,访问Servlet