Spring Boot FreeMarker 使用教程
FreeMarker 跟 Thymeleaf 一样,是一种模板引擎,他可以无缝兼容 FreeMarker 在 Spring Boot 开发者中仍然有着很高的地位。
本章重点内容
- 编写一个最简单的 Freemark 模板示例
- 简单说明 FreeMarker
1 FreeMarker 简介
相对于 Jsp ,FreeMarker 具有太多的优势。FreeMarker 适合 Mvc 场景。
FreeMarker 最大的特点就是具有可编程能力,可以对任何后台输出的数据做编程能力,这就像在 Java 中加入了 PHP 功能,这非常有趣。
FreeMarker 支持各类语法包括 字符输出、条件判断 if/else、循环遍历、
1.1 变量
${...}
1.2 条件语句
<#if condition>
...
<#elseif condition2>
...
<#elseif condition3>
...
<#else>
...
</#if>
1.3 循环语句
假设 users 包含['Joe', 'Kate', 'Fred'] 序列:
<#list users as user>
<p>${user}
</#list>
输出:
<p>Joe
<p>Kate
<p>Fred
1.4 include 包含语句
将版权信息单独存放在页面文件 copyright_footer.html 中:
<hr>
<i>
Copyright (c) 2000 <a href="http://www.baidu.com">Baidu Inc</a>,
<br>
All Rights Reserved.
</i>
当需要用到这个文件时,可以使用 include 指令来插入:
<html>
<head>
<title>Test page</title>
</head>
<body>
<h1>Test page</h1>
<p>Blah blah...
<#include "/copyright_footer.html">
</body>
</html>
2 Spring Boot 中编写一个 FreeMarker 示例
本示例文件结构, 新增了连个用于示例文件的文件 IndexController.java 与 index.ftl
+ java/fishpro/freemarker/controller
-- IndexController.java
+ resources/templates
--index.ftl
2.1 新建一个 Spring Boot 的 Maven 项目
2.1 新建 Spring Boot 项目
- File > New > Project,如下图选择
Spring Initializr
然后点击 【Next】下一步 - 填写
GroupId
(包名)、Artifact
(项目名) 即可。点击 下一步
groupId=com.fishpro
artifactId=freemarker - 选择依赖
Spring Web Starter
前面打钩, 在模板列中勾选apache freemarker
。 - 项目名设置为
spring-boot-study-freemarker
.
2.2 编辑 Pom.xml 引入依赖
注意如果在新建下面的时候没有引入 FreeMarker 那么就在这里复制粘贴上去。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.fishpro</groupId>
<artifactId>freemarker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>freemarker</name>
<description>Demo project for Spring Boot</description>
<span class="hljs-tag"><<span class="hljs-name">properties</span>></span>
<span class="hljs-tag"><<span class="hljs-name">java.version</span>></span>1.8<span class="hljs-tag"></<span class="hljs-name">java.version</span>></span>
<span class="hljs-tag"></<span class="hljs-name">properties</span>></span>
<span class="hljs-tag"><<span class="hljs-name">dependencies</span>></span>
<span class="hljs-tag"><<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>org.springframework.boot<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>spring-boot-starter-freemarker<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>org.springframework.boot<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>spring-boot-starter-web<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>org.springframework.boot<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>spring-boot-starter-test<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">scope</span>></span>test<span class="hljs-tag"></<span class="hljs-name">scope</span>></span>
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"></<span class="hljs-name">dependencies</span>></span>
<span class="hljs-tag"><<span class="hljs-name">build</span>></span>
<span class="hljs-tag"><<span class="hljs-name">plugins</span>></span>
<span class="hljs-tag"><<span class="hljs-name">plugin</span>></span>
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>org.springframework.boot<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>spring-boot-maven-plugin<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
<span class="hljs-tag"></<span class="hljs-name">plugin</span>></span>
<span class="hljs-tag"></<span class="hljs-name">plugins</span>></span>
<span class="hljs-tag"></<span class="hljs-name">build</span>></span>
</project>
2.3 配置文件
注意 Spring Boot 中默认不需要做任何配置,示例程序把默认的端口 8080 改为了 8086,并把 application.properties 改为了 application.yml
server:
port: 8086
2.4 编写示例代码
本示例文件结构, 新增了连个用于示例文件的文件 IndexController.java 与 index.ftl
IndexController.java
/**
* 在是一个普通的 Controller 类
* */
@Controller
public class IndexController {
<span class="hljs-comment">/**
* 路由 /index
* 返回 index 这里默认配置自动映射到 templages/index
* */</span>
<span class="hljs-meta">@RequestMapping("/index")</span>
<span class="hljs-keyword">public</span> String <span class="hljs-title function_">index</span><span class="hljs-params">(Model model)</span>{
model.addAttribute(<span class="hljs-string">"welcome"</span>,<span class="hljs-string">"hello fishpro"</span>);
<span class="hljs-keyword">return</span> <span class="hljs-string">"index"</span>;
}
}
index.ftl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
this is welcome ${welcome}
</body>
</html>
2.5 运行示例
在浏览器中输入 http://localhost:8086/index 显示为,hello fishpro 就是后台输出的 model 对象
this is welcome hello fishpro
2.6 FreeMarker 实际应用场景
FreeMarker 比传统的 JSP 多出一个模板的作用,就是说,我们可以做多个不同样式的模板,动态的切换。这就是 FreeMarker 应用场景。