FreeMarker入门-第一讲

1.简介

FreeMarker 是一个用 Java 语言编写的模板引擎,它基于模板来生成文本输出。 FreeMarker Web 容器无关,即在 Web 运行时,它并不知道 Servlet HTTP 。它不仅可以用作表现层的实现技术,而且还可以用于生成 XML JSP Java 等,目前企业中:主要用Freemarker做静态页面或是页面展示
这里写图片描述

在Maven中使用它:
<dependency>
<groupId>freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.9</version>
</dependency>

2.第一个FreeMarker程序(输出到控制台)


FreeMarkerDemo:
@Test//模板数据输出到控制台
	public  void print01() throws Exception {
		//1:实例化Freemarker的配置类
        Configuration conf = new Configuration();
        //2:设置模板所在目录
        String dir = "F:\\dqwork\\freemarker\\ftl\\";
        conf.setDirectoryForTemplateLoading(new File(dir));
        //3:根据名称获取模板
        Template template = conf.getTemplate("01.ftl");
        //4.定义数据模型
        Map<String,Object> root = new HashMap<String,Object>();
        root.put("username", "zhangsan");
        //5.给模板设置数据模型
        template.process(root, new PrintWriter(System.out));
	}


freemarker.ftl
<!DOCTYPE HTML>
<html>
  <head>
    <title>freeMarker</title>
      <meta charset="utf-8">
  </head>
  
  <body>
    ${username}
  </body>
</html>

第二个FreeMarker程序(输出到文件)

@Test//模板数据输出到html文件-传递简单字符串
	public  void print02() throws Exception {
		//1:实例化Freemarker的配置类
        Configuration conf = new Configuration();
        //2:设置模板所在目录
        String dir = "F:\\dqwork\\freemarker\\ftl\\";
        conf.setDirectoryForTemplateLoading(new File(dir));
        //3:根据名称获取模板
        Template template = conf.getTemplate("02.ftl");
        //4.定义数据模型
        Map<String,Object> root = new HashMap<String,Object>();
        root.put("username", "zhangsan");
        //5.给模板设置数据模型
        template.process(root, new PrintWriter(new File("F:\\dqwork\\freemarker\\html\\02.html")));
	}


第三个FreeMarker程序(模型定义为对象)

@Test//模板数据输出到html文件-传递对象
	public  void print03() throws Exception {
		//1:实例化Freemarker的配置类
        Configuration conf = new Configuration();
        //2:设置模板所在目录
        String dir = "F:\\dqwork\\freemarker\\ftl\\";
        conf.setDirectoryForTemplateLoading(new File(dir));
        //3:根据名称获取模板
        Template template = conf.getTemplate("03.ftl");
        //4.定义数据模型
        Map<String,Object> root = new HashMap<String,Object>();
        root.put("persion", new Persion("张三",25));
        //5.给模板设置数据模型
        template.process(root, new PrintWriter(new File("F:\\dqwork\\freemarker\\html\\03.html"),"utf-8"));
	}
<!DOCTYPE HTML>
<html>
  <head>
    <title>freeMarker</title>
	<meta charset="utf-8">
  </head>
  <body>
    <h1>${persion.name}-----${persion.age}</h1>
    
  </body>
</html>

第三个FreeMarker程序(模型定义为集合)

	@Test//模板数据输出到html文件-传递集合
	public  void print04() throws Exception {
		//1:实例化Freemarker的配置类
        Configuration conf = new Configuration();
        //2:设置模板所在目录
        String dir = "F:\\dqwork\\freemarker\\ftl\\";
        conf.setDirectoryForTemplateLoading(new File(dir));
        //3:根据名称获取模板
        Template template = conf.getTemplate("04.ftl");
        //4.定义数据模型
        List<Object> persions = new ArrayList<Object>();
        persions.add(new Persion("张三",25));
        persions.add(new Persion("李四",26));
        Map<String,Object> root = new HashMap<String,Object>();
        root.put("persions", persions);
        //5.给模板设置数据模型
        template.process(root, new PrintWriter(new File("F:\\dqwork\\freemarker\\html\\04.html"),"utf-8"));
	}
<!DOCTYPE HTML>
<html>
  <head>
    <title>freeMarker</title>
	<meta charset="utf-8">
  </head>
  
  <body>
    <#list persions as persion>
		${persion.name}---${persion.age}<br/>
	</#list>
  </body>
</html>





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郝文龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值