FreeMarker简介
FreeMarker是一个非常古老的模板引擎,可以在Web环境或者非Web环境中。与Thymeleaf不同,FreeMarker需要经过解析才能够在浏览器中展示出来。FreeMarker不仅可以用来配置HTML页面模板,也可以作为电子邮件模板、配置文件模板以及源码模板等。
SpringBoot整合FreeMarker步骤
1、创建项目,添加依赖
首先创建Spring Boot项目,然后添加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.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.learn</groupId>
<artifactId>thymeleaf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>thymeleaf</name>
<description>thymeleaf project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- FreeMarker依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、配置FreeMarker
开发者可以在application.properties中对这些默认的配置进行修改,部分常见配置如下:
#对默认的FreeMarker
#HttpServletRequest的属性是否可以覆盖controller中的model的同名项
spring.freemarker.allow-request-override=false
#HttpSession的属性是否可以覆盖controller中model的同名项
spring.freemarker.allow-session-override=false
#是否开启缓存
spring.freemarker.cache=false
#模板文件编码
spring.freemarker.charset=UTF-8
#是否检查模板位置
spring.freemarker.check-template-location=true
#Content-Type的值
spring.freemarker.content-type=text/html
#是否将HttpServletRequest中的属性添加到Model中
spring.freemarker.expose-request-attributes=false
#是否将HttpSession中的属性添加到Model中
spring.freemarker.expose-session-attributes=false
#模板文件后缀
spring.freemarker.suffix=.ftl
#模板文件位置
spring.freemarker.template-loader-path=classpath:/templates/
3、设置Book实体类
package com.learn.thymeleaf.entity;
public class Book {
private Integer id;
private String name;
private String author;
public Book(Integer id, String name, String author) {
this.id = id;
this.name = name;
this.author = author;
}
public Book() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
4、配置控制器
package com.learn.thymeleaf.controller;
import com.learn.thymeleaf.entity.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.List;
@Controller
public class Book2Controller {
@GetMapping("/bookfree")
public ModelAndView bookfree(){
List<Book> books= new ArrayList<Book>();
Book b1=new Book();
b1.setId(3);
b1.setName("西游记");
b1.setAuthor("吴承恩");
Book b2=new Book();
b2.setId(4);
b2.setName("水浒传");
b2.setAuthor("施耐庵");
books.add(b1);
books.add(b2);
ModelAndView mv=new ModelAndView();
mv.addObject("bookfree",books);
mv.setViewName("bookfree");
return mv;
}
}
5、创建视图
按照配置文件,在resources/templates目录下创建bookfree.ftl文件,内容如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图书列表</title>
</head>
<body>
<table border="1">
<tr>
<td>图书编号</td>
<td>图书名称</td>
<td>图书作者</td>
</tr>
<#if bookfree ??&&(bookfree?size>0)>
<#list bookfree as book>
<tr>
<td>${book.id}</td>
<td>${book.name}</td>
<td>${book.author}</td>
</tr>
</#list>
</#if>
</table>
</body>
</html>
代码解释:
- 第14行首先判断model中的bookfree不为空并且bookfree中有数据,然后进行遍历。
- 第15~21行表示遍历bookfree集合,将集合中的数据通过表格展示出来。
6、运行
运行服务器,在浏览器地址中输入“http://localhost:8080/bookfree”,即可看到运行结果: