SpringBoot中的Thymeleaf模板

一、前言

Thymeleaf 的出现是为了取代 JSP,虽然 JSP 存在了很长时间,并在 Java Web 开发中无处不在,但是它也存在一些缺陷:

1、JSP 最明显的问题在于它看起来像HTML或XML,但它其实上并不是。大多数的JSP模板都是采用HTML的形式,但是又掺杂上了各种JSP标签库的标签,使其变得很混乱。

2、JSP 规范是与 Servlet 规范紧密耦合的。这意味着它只能用在基于 Servlet 的Web应用之中。JSP模板不能作为通用的模板(如格式化Email),也不能用于非Servlet的 Web 应用。

相较于 JSP 来说,Thymeleaf 很好的解决了这些缺点:

1、Thymeleaf模板是原生的,不依赖于标签库。它能在接受原始 HTML 的地方进行编辑和渲染。

2、因为它没有与Servlet规范耦合,因此 Thymeleaf 模板能够进入JSP所无法涉足的领域。这意味着Thymeleaf模板与JSP不同,它能够按照原始的方式进行编辑甚至渲染,而不必经过任何类型的处理器。当然,我们需要Thymeleaf来处理模板并渲染得到最终期望的输出。即便如此,如果没有任何特殊的处理,home.html也能够加载到Web浏览器中,并且看上去与完整渲染的效果很类似。

Spring boot不建议使用 JSP 开发web。

二、集成 Thymeleaf 模板引擎

SpringBoot 对 Thymeleaf 模板引擎的支持也很简单:

1、pom.xml

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

这时候,SpringBoot 对 Thymeleaf 模板的支持就完成了,我们就能在 Web 开发中使用 Thymeleaf 模板了,简单吧?

之前的文章有提到 SpringBoot 的关键是 “约定俗成”。既然我们选择了这么简单的配置,那么在开发中就要遵守 SpringBoot 对 Thymeleaf 约定俗成的方案,最重要的一点就是 模板文件放在 templates 目录下,即模板解析器前缀是 /templates/ ,后缀是 .html 。

2、application.yml

如果不想要所谓约定俗成的方案,想进行一些自定义的配置呢?且看下方:

spring:
 thymeleaf:
 prefix: classpath:/templates/
 suffix: .html
 servlet:
  content-type: text/html
 enabled: true
 encoding: UTF-8
 mode: HTML5
 cache: false

3、WebConfig.java

如果上面的配置还不能达到你的要求,你想要更细化对 Thymeleaf 的控制,包括配置视图解析器、模板解析器以及模板引擎这些,那么请看下面的方案!

/**
 * 1、ThymeleafViewResolver 接收逻辑视图名称将它解析为视图
 * 2、SpringTemplateEngine会在Spring中启用Thymeleaf引擎,用来解析模板,并基于这些模板渲染结果
 * 3、TemplateResolver会最终定位和查找模板。
 */
@Configuration
public class WebConfig {
 /**
  * 配置 Thymeleaf 视图解析器 —— 将逻辑视图名称解析为 Thymeleaf 模板视图
  *
  * @param springTemplateEngine 模板引擎
  * @return
  */
 @Bean
 public ViewResolver viewResolver(SpringTemplateEngine springTemplateEngine){
  ThymeleafViewResolver resolver = new ThymeleafViewResolver();
  resolver.setTemplateEngine(springTemplateEngine);
  return resolver;
 }
 /**
  * 模板引擎 —— 处理模板并渲染结果
  *
  * @param templateResolver 模板解析器
  * @return
  */
 @Bean
 public SpringTemplateEngine springTemplateEngine(ITemplateResolver templateResolver) {
  SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
  springTemplateEngine.setTemplateResolver(templateResolver);
  return springTemplateEngine;
 }
 /**
  * 模板解析器 —— 加载 Thymeleaf 模板
  *
  * @return
  */
 @Bean
 public ITemplateResolver templateResolver() {
  SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
  templateResolver.setPrefix("classpath:/templates/");
  templateResolver.setSuffix(".html");
  templateResolver.setTemplateMode(TemplateMode.HTML);
  templateResolver.setCacheable(false);
  templateResolver.setTemplateMode("HTML5");
  return templateResolver;
 }
}

三、使用 Thymeleaf 模板

做好了上面的配置后,让我们来看看如何在 SpringBoot 中使用 Thymeleaf 模板吧:

1、模板文件 — /templates/user/list.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
 <meta charset="UTF-8" />
 <title>Insert title here</title>
</head>
<body>
<h2>用户列表</h2>
<div>
 <ul>
  <li th:each="user:${users}">
   <span th:text="${user.uuid}"></span>-
   <span th:text="${user.name}"></span>-
   <span th:text="${user.age}"></span>-
   <span th:text="${user.address}"></span>
  </li>
 </ul>
</div>
</body>
</html>

2、控制层 — ModelAndViews

这里 Model 指的是:控制层处理完请求,返回需要渲染的结果;Views 指的是:模板的逻辑视图名(前后端分离)。

@Controller
@RequestMapping("/user")
public class UserController {
 @RequestMapping("/list")
 public String listUser(Model model) {
  List<UserDto> userList = new ArrayList<>();
  for (int i = 0; i < 10; i++) {
   userList.add(new UserDto(UUID.randomUUID().toString().replace("-", ""), "张三" + i, 1, "中国北京"));
  }
  model.addAttribute("users", userList);
  return "user/list";
 }
}

3、效果

演示源代码:https://github.com/JMCuixy/Thymeleaf

总结

以上所述是小编给大家介绍的SpringBoot中的Thymeleaf模板,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • Spring Boot使用Thymeleaf + Gradle构建war到Tomcat

    Spring Boot 以Jar的方式部署启动,这个不用介绍了, 之前也介绍了关于 Spring Boot + thymeleaf 的简单使用 ,但是今天遇到一个问题, 我先描述下问题的场景: 由于运维部门的需求,项目需要以war的形式放到tomcat运行 ,而不是原定的jar的方式运行 配置了一下午,也查了一下午的资料,以war的方式在Tomcat能运行,并且能访问Controller,但是在返回html视图时,找不到视图模板.最终发现问题在Thymeleaf的配置,话不多说,具体看操作步骤:

  • springboot如何使用thymeleaf模板访问html页面

    引言 在传统的web开发中通常使用jsp页面,首先需要在pom文件中引入springmvc相关的包,然后写springmvc的配置文件(包括访问资源的路径解析),之后还需再web.xml中配置访问路由.这无疑太麻烦了,每次开发前都需要编写大量的配置文件. springboot为此提供了高效便捷的解决方案,只需再pom.xml中添加web开发的依赖,便可进行web开发,省去了繁琐的配置步骤. 下面为web开发引入的依赖 <dependency> <groupId>org.spring

  • 详解SpringBoot+Thymeleaf 基于HTML5的现代模板引擎

    序言: Thymeleaf 是Java服务端的模板引擎,与传统的JSP不同,前者可以使用浏览器直接打开,因为可以忽略掉拓展属性,相当于打开原生页面,给前端人员也带来一定的便利.如果你已经厌倦了JSP+JSTL的组合,Thymeleaf或许是个不错的选择!本工程传送门:SpringBoot-Web-Thymeleaf 开始使用 1.引入依赖 SpringBoot默认提供了Thymeleaf的Starter,只需简单引入依赖即可. <dependency> <groupId>org.s

  • IDEA+maven+SpringBoot+JPA+Thymeleaf实现Crud及分页

    一.开发环境: 1.windows 7 企业版 2.IDEA 14 3.JDK 1.8 4.Maven 3.5.2 5.MariaDB 6.SQLYog 二.Maven设置: Maven目录下的conf目录下的settings.xml做如下内容的添加: 1.使用阿里云的仓库,比官网访问速度快很多 <mirror> <id>nexus-aliyun</id> <mirrorOf>central</mirrorOf> <name>Nexu

  • Spring Boot + thymeleaf 实现文件上传下载功能

    最近同事问我有没有有关于技术的电子书,我打开电脑上的小书库,但是邮件发给他太大了,公司又禁止用文件夹共享,于是花半天时间写了个小的文件上传程序,部署在自己的Linux机器上. 提供功能: 1 .文件上传 2.文件列表展示以及下载 原有的上传那块很丑,写了点js代码优化了下,最后界面显示如下图: 先给出成果,下面就一步步演示怎么实现. 1.新建项目 首先当然是新建一个spring-boot工程,你可以选择在网站初始化一个项目或者使用IDE的Spring Initialier功能,都可以新建一个项目

  • spring boot thymeleaf 图片上传web项目根目录操作步骤

    thymeleaf介绍 简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下三个极吸引人的特点: 1.Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果.这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式.浏览器解释 html 时会忽略未定义的标签属性,所以 t

  • Spring boot搭建web应用集成thymeleaf模板实现登陆

    Spring boot 搭建web应用集成了thymeleaf模板实现登陆 下面是pom.xml的配置 <?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:schema

  • Spring Boot配置Thymeleaf(gradle)的简单使用

    最近项目用到了Spring Boot ,但是在控制器返回html视图并渲染参数的时候,存在了疑问.后面考虑用Thymeleaf ,感觉真的不错,下面分享给大家 总共四步: jar 引入 控制器参数传递 html标签引入 Thymeleaf 缓存设置 一.相关Jar的引用 1.maven的引用方式: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-

  • SpringMVC中使用Thymeleaf模板引擎实例代码

    本文研究的主要是SpringMVC中使用Thymeleaf模板引擎的相关内容,具体介绍如下. Thymeleaf提供了一组Spring集成,允许您将其用作Spring MVC应用程序中全面替代JSP的功能. Maven依赖 <!-- thymeleaf-spring4 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifa

  • spring boot+thymeleaf+bootstrap实现后台管理系统界面

    最近在学spring boot ,学习一个框架无非也就是使用它来做以前做的事情,两者比较才有不同,说一下自己使用的体会. 先来说下spring boot ,微框架.快速开发,相当于零配置,从一个大神那看来的说:spring boot 相当于框架的框架 ,就是集成了很多,用哪个添加哪个的依赖就行,这样的话自己看不到配置,对于习惯了使用配置刚使用spring boot的开发者来说可能还有点不习惯,什么都不用配,看不到配置感觉对项目整体架构有点陌生,再说在spring boot 中使用 thymele

随机推荐