SpringBoot整合Thymeleaf与FreeMarker视图层技术

Karli ·
更新时间:2024-09-21
· 779 次阅读

目录

整合Thymeleaf

1. 创建工程添加依赖

2. 配置Thymeleaf

3. 配置控制器

4. 创建视图

5. 运行

整合FreeMarker

1. 创建项目添加依赖

2. 配置FreeMarker

3. 控制器

4. 创建视图

5. 运行

整合Thymeleaf

Thymeleaf是新一代Java模板引擎,类似于Velocity、FreeMarker等传统Java模板引擎。与传统Java模板引擎不同的是,Thymeleaf支持HTML原型,既可以让前端工程师在浏览器中直接打开查看样式,也可以让后端工程师结合真实数据查看显示效果。同事,Spring Boot提供了Thymeleaf自动化配置解决方案,因此在Spring Boot中使用Thymeleaf 非常方便。Spring Boot整合Thymeleaf 主要可通过如下步骤

1. 创建工程添加依赖

新建一个Spring Boot工程,然后添加spring-boot-starter-web 和spring-boot-starter-thymeleaf 依赖

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 整合Thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> 2. 配置Thymeleaf

Spring Boot为Thymeleaf提供了自动化配置类ThymeleafAutoConfiguration,相关的配置属性在ThymeleafProperties 类中,ThymeleafProperties类部分源码如下:

@ConfigurationProperties(prefix = "spring.thymeleaf") public class ThymeleafProperties { private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8; public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html"; }

由此配置可以看到,默认的模板位置在classpath:/templates/,默认的模板后缀名为.html。如果使用IDEA创建Spring Boot 项目,templates文件夹默认会创建。如需对默认的Thymeleaf 配置参数进行自定义配置,可以在application.properties 中进行配置,部分常见配置如下:

#是否开启缓存,开发时可设置为false,默认为true
spring.thymeleaf.cache=false
#检查模版是否存在,默认为true
spring.thymeleaf.check-template=true
#检查模版位置是否存在,默认为true
spring.thymeleaf.check-template-location=true
#模版文件编码
spring.thymeleaf.encoding=UTF-8
#模版文件位置
spring.thymeleaf.prefix=classpath:/templates/
#Content-Type配置
spring.thymeleaf.servlet.content-type=text/html
#模版文件后缀
spring.thymeleaf.suffix=.html

3. 配置控制器

创建Book实体类,然后在Controller中返回ModelAndView,如下:

public class Book { private int id; private String name; private String author; public int getId() { return id; } public void setId(int 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; } } @RestController public class BookController { @GetMapping(value = "/books") public ModelAndView books(){ List<Book> books = new ArrayList<>(); Book b1 = new Book(); b1.setId(1); b1.setAuthor("唐家三少"); b1.setName("斗罗大陆Ⅰ"); Book b2 = new Book(); b2.setId(2); b2.setAuthor("唐家三少"); b2.setName("斗罗大陆Ⅱ"); books.add(b1); books.add(b2); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("books",books); modelAndView.setViewName("books"); return modelAndView; } } 4. 创建视图

在resources目录下的templates目录中创建books.html,如下:

<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>图书列表</title> </head> <body> <table border="1"> <tr> <td>图书编号</td> <td>图书名称</td> <td>图书作者</td> </tr> <tr th:each="book:${books}"> <td th:text="${book.id}"></td> <td th:text="${book.name}"></td> <td th:text="${book.author}"></td> </tr> </table> </body> </html>

代码解释:

首先在第二行导入Thymeleaf 的名称空间

通过遍历将books中的数据展示出来,Thymeleaf中通过th:each进行集合遍历,通过th:text展示数据

5. 运行

浏览器输入"http://localhost:8081/books",查看运行结果,如图:

整合FreeMarker

FreeMarker 是一个非常古老的模板引擎,可以用在Web环境或者非Web环境中。FreeMarker 需要经过解析才能在浏览器中展示出来。FreeMarker 不仅可以用来配置HTML页面模板,也可以作为电子邮件模板、配置文件模板以及源码模板。整合步骤如下:

1. 创建项目添加依赖

创建Spring Boot项目,然后添加spring-boot-starter-web和spring-boot-starter-freemarker依赖,如下:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 整合FreeMarker --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> 2. 配置FreeMarker

Spring Boot对FreeMarker 也提供了自动化配置类FreeMarkerAutoConfiguration,相关的配置属性在FreeMarkerProperties中,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 = ".ftl"; ... }

FreeMarker 默认模板位置和Thymeleaf 一样,都在classpath:/templates/中,默认文件后缀是.ftl,开发者可以在application.properties 中对这些默认配置进行修改,如下:

3. 控制器

控制器和Thymeleaf 中的控制器一样,这里不再重复

4. 创建视图

在resources目录下的templates目录中创建books.ftl 文件,如下:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>图书列表FreeMarker</title> </head> <body> <table border="1"> <tr> <td>图书编号</td> <td>图书名称</td> <td>图书作者</td> </tr> <#if books ?? && (books?size>0)> <#list books as book> <tr> <td>${book.id}</td> <td>${book.name}</td> <td>${book.author}</td> </tr> </#list> </#if> </table> </body> </html>

代码解释:

先判断model中的books部位可控并且books中有数据,然后再进行遍历

5. 运行

浏览器输入"http://localhost:8081/books",查看运行结果,如图:

到此这篇关于SpringBoot整合Thymeleaf与FreeMarker视图层技术的文章就介绍到这了,更多相关SpringBoot整合视图层内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



thymeleaf freemarker springboot

需要 登录 后方可回复, 如果你还没有账号请 注册新账号