1.什么是FreeMarker?
以下是引用官网的说明介绍:
Apache FreeMarker™ 是一个模板引擎:一个基于模板和变化的数据生成文本输出(HTML 网页、电子邮件、配置文件、源代码等)的 Java 库。模板用 FreeMarker 模板语言 (FTL) 编写,这是一种简单的专用语言(不是像 PHP 这样成熟的编程语言)。通常,使用通用编程语言(如 Java)来准备数据(发出数据库查询、进行业务计算)。然后,Apache FreeMarker 使用模板显示准备好的数据。在模板中,您关注的是如何呈现数据,而在模板之外,您关注的是呈现哪些数据。
FreeMarker 最初是为在 MVC web 应用程序框架中生成 HTML 页面而创建的,但它并不绑定到 servlets 或 HTML 或任何与 web 相关的东西。只要使用FreeMarker的标记语言编写成模板,然后就可以直接使用它的类库来实现模板加工和输出。
2.常用GAV
- FreeMarker的maven
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
- springboot中的starter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<version>spring-boot-version</version>
</dependency>
3.spring-boot使用FreeMarker示例
spring-boot默认使用的模板引擎是Thymeleaf,通过在项目中添加FreeMarker的starter,可以自动切换为FreeMarker。
- 创建一个springboot-web项目引入spring-boot-starter-web和spring-boot-starter-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 https://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.3.11.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lwf</groupId>
<artifactId>demo-freemarker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-freemarker</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 创建项目之后,不需要做额外配置,直接在resources/templates路径下创建ftlh文件,这里注意在springboot 2.x版本中的freemarker文件的后缀是ftlh,相关的信息在spring-boot-autoconfigure包的
FreeMarkerProperties
中
@ConfigurationProperties(prefix = "spring.freemarker")
public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties {
public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/";
public static final String DEFAULT_PREFIX = "";
public static final String DEFAULT_SUFFIX = ".ftlh";
}
我们创建hello.ftlh
文件如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>${title}</title>
</head>
<body>
say hello to ${who}
</body>
</html>
- 接下来创建controller
@Controller
public class DemoController {
@RequestMapping("/hello")
public String sayHello(Model model){
model.addAttribute("title","HELLO FROM WEB");
model.addAttribute("who","xiaoming");
return "hello";
}
}
我们运行springboot,直接在浏览器输入http://localhost:8080/hello,获取网页输出:
至此,springboot使用FreeMarker作为模板引擎的小示例完成。