FreeMarker入门-第一讲

xiaoxiao2021-02-28  8

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>
转载请注明原文地址: https://www.6miu.com/read-1900283.html

最新回复(0)