作者:卖女孩的小方子 | 来源:互联网 | 2024-10-21 16:08
目录Freemarker简介安装使用FIL取值list迭代列表list迭代Map内建函数Freemarker与Servlet整合Freemarker简介FreeMarker是一款主
目录
- Freemarker
- 简介
- 安装使用
- FIL取值
- list迭代列表
- list迭代Map
- 内建函数
- Freemarker与Servlet整合
Freemarker
简介
- FreeMarker 是一款 主流的模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。
- 模板引擎的目标是数据+模板=结果
- 模板编写为FreeMarker Template Language (FTL)。它是简单的,专用的语言,
安装使用
下载地址:http://mirror.bit.edu.cn/apache/freemarker/engine/2.3.28/binaries/apache-freemarker-2.3.28-bin.tar.gz
使用步骤:
FreemarkerDemo1.java
//创建核心配置对象
Configuration cOnfiguration= new Configuration(Configuration.VERSION_2_3_28);
//设置加载目录
configuration.setClassForTemplateLoading(FreemarkerDemo1.class,"");
//得到模板对象
Template t = configuration.getTemplate("demo2.ftl");
//创建数据
Map data = new HashMap();
List 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 data = new HashMap();
List 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 data = new HashMap();
List 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文件
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">
freemarker
freemarker.ext.servlet.FreemarkerServlet
TemplatePath
/WEB-INF/ftl
freemarker
*.ftl
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
<#list students as student>
学号:${student.sno}-----姓名:${student.name}
#list>