热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Freemarker

目录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迭代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}

内建函数

参考手册: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



  • 编写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 students= new ArrayList<>();
for (int i = 0; i <5; i++) {
Map 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}


  • 启动Tomcat,访问Servlet


推荐阅读
author-avatar
卖女孩的小方子
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有