Spring Boot Thymeleaf 模板引擎的使用

Spring Boot 中可以支持很多模板引擎,Thymeleaf 是 Spring Boot 官方推荐使用的模板引擎,虽然在社区 Thymeleaf 的性能被许多人所吐糟,但这仍然不影响大量的开发人员使用他。

Thymeleaf 是后台开发的最佳实践

当前 Spring Boot 2.0 及以后版本已经支持 Thymeleaf 3.0

本章讲解如何在 Spring Boot 中使用 Themealf.

源码下载

欢迎关注我的微信公众号 程序鱼 ,我们一起编程聊天看世界。

1 创建一个 Spring Boot 工程

1)File > New > Project,如下图选择 Spring Initializr 然后点击 【Next】下一步

2)填写 GroupId(包名)、Artifact(项目名) 即可。点击 下一步
groupId=com.fishpro
artifactId=thymeleaf

3)选择依赖 Spring Web Starter 前面打钩,选择模板,在 Thymeleaf 依赖前面打钩

4)项目名设置为 ·spring-boot-study-thymeleaf。

2 引入依赖

如果在创建项目的时候已经引入依赖,则不需要此步骤,打开根目录下的文件 pom.xml dependencies 节点加入以下代码

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

3 配置Thymeleaf

找到src\main\resources\application.yml,如果是application.properties 更名后缀yml 即可,当然习惯使用 properties 后缀的则不需要更改。
注意这里的配置不是必须的,不配做,thymeleaf则有默认的配置。

server:
  port: 8083
#thymelea模板配置
spring:
  thymeleaf:
    #thymeleaf 所在路径
    prefix: classpath:/templates/
    #thymeleaf 后缀
    suffix: .html
    #thymeleaf 采用的标准
    mode: HTML5
    #thymeleaf 编码格式
    encoding: UTF-8

application.properties 后缀格式 表示为 spring.thymeleaf.prefix=classpath:/templates/ 其他类似修改。

4 编写代码实例

4.1 项目结构

在编写代码之前应该搞清楚 thymeleaf 结构。
src\main\resources\templates 为目录的 thymeleaf 模板存放路径

4.2 数据准备

  1. 新建Java文件 src\main...\controller\IndexController.java
    在 IndexController 增加展示层数据的输出
    UserDTO.java 用于测试的实体类
public class UserDTO {
    private String username;
    private String sex;
    private Date birthday;

    public UserDTO(){}

    public UserDTO(String username,String sex,Date birthday){
        this.username=username;
        this.sex=sex;
        this.birthday=birthday;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

IndexController.java 用于测试的控制层

@Controller
public class IndexController {
    @RequestMapping("/sample")
    public String sample(Model model){
        model.addAttribute("user",getUserDTOData());
        List<UserDTO> users=new ArrayList<>();
        users.add(new UserDTO("zhangsan","男",new Date()));
        users.add(new UserDTO("wangjingjing","女",new Date()));
        users.add(new UserDTO("limeimei","女",new Date()));
        users.add(new UserDTO("lisi","男",new Date()));
        model.addAttribute("users",users);
        return "/index/sample";
    }

    /**
     * 构造一个user对象
     * */
    private UserDTO getUserDTOData(){
        UserDTO userDTO=new UserDTO();
        userDTO.setUsername("fishpro");
        userDTO.setSex("男");
        userDTO.setBirthday(new Date());
        return  userDTO;
    }
}
  1. 新建模板文件src\main\resources\templates\index\sample.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Thymeleaf简单示例</title>
</head>
<body>
<p style="background-color: #c7ddef">
    ${...} 可以显示后台传输的变量
</p>
<p th:text="${user.username}"></p>
<p style="background-color: #c7ddef">
    th:each循环标签
</p>
<p th:each=" userobject : ${users}">
    <span th:text="${userobject.username}"></span>
</p>
</body>
</html>

5 运行实例

在浏览器中输入 http://localhost:8083/demo/simple



欢迎关注我的微信公众号,我们一起编程聊天看世界

原文地址:https://www.cnblogs.com/fishpro/p/11175391.html

时间: 2024-08-29 14:25:45

Spring Boot Thymeleaf 模板引擎的使用的相关文章

spring boot Thymeleaf模板引擎 最简单输出例子

spring boot  Thymeleaf模板引擎  最简单输出例子 控制器代码如下: @GetMapping(value = "/test")public String test(Model model){ List<Boy> boy = new ArrayList<Boy>(); boy.add(new Boy("xx",11)); boy.add(new Boy("yy",22)); boy.add(new Boy

Spring Boot 整合模板引擎 Freemaker、thymeleaf

1.常用的 Spring Boot 2.x 模板引擎和官方推荐案例 1)JSP(后端渲染,消耗性能) Java Server Pages 动态网页技术,由应用服务器中的 JSP 引擎来编译和执行,再将生成的整个页面返回给客户端.优点是:可以写java代码.支持表达式语言(el.jstl).内建函数. 但 JSP 本质上是 Servlet,它非常占用 JVM 内存.Java Web 官方推荐,但 Spring Boot 不推荐https://docs.spring.io/spring-boot/d

spring boot + thymeleaf 3 国际化

** 原创文章,请勿转载 ** 在给spring boot 1.5.6 + thymeleaf 3进行国际化时,踩了一个坑(其实不止一个). 现象: 看到了吧, 就是取值的key, 后面被加了_en_US 或 _zh_CN, 以及前后的问号. 先看下代码,首先两个资源文件: messages_en_US.properties page.content=this is a test string. message_zh_CN.properties, 在eclipse里找开的,内容是: 这是一个测试

(二)SpringBoot基础篇- 静态资源的访问及Thymeleaf模板引擎的使用

一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf模板引擎,可以使用模板引擎进行渲染处理,默认版本为2.1,可以重新定义Thymeleaf的版本号,在maven的配置文件中配置如下内容: <properties> <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version> &l

Thymeleaf 模板引擎简介

目录 Thymeleaf 模板引擎 官方文档下载 Hello World 新建应用 后台控制器 前端页面 浏览器访问测试 Thymeleaf 模板引擎1.Thymeleaf 是 Web 和独立环境的现代服务器端 Java 模板引擎,能够处理HTML,XML,JavaScript,CSS 甚至纯文本. 2.Thymeleaf 的主要目标是提供一种优雅和高度可维护的创建模板的方式.为了实现这一点,它建立在自然模板的概念上,将其逻辑注入到模板文件中,不会影响模板被用作设计原型.这改善了设计的沟通,弥补

SpringBoot 配置 Thymeleaf模板引擎

Thymeleaf模板引擎 什么是模板引擎 模板引擎(这里特指用于Web开发的模板引擎)是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档. 学习视频: http://www.itlaoqi.com/chapter/1688.html 源码地址: QQ群 814077650 , 群共享中自助下载 老齐的官网: itlaoqi.com (更多干货就在其中) Thymeleaf的特点 Thymeleaf优点 主流唯一的前后端通用

SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web视图

在Web开发过程中,Spring Boot可以通过@RestController来返回json数据,那如何渲染Web页面?Spring Boot提供了多种默认渲染html的模板引擎,主要有以下几种: Thymeleaf FreeMarker Velocity Groovy Mustache Spring Boot 推荐使用这些模板引擎来代替 Jsp,Thymeleaf 只是其中一种,下面我们来简单聊聊Thymeleaf及实践一下如何整合Spring Boot和Thymeleaf. 1.Thyme

Thymeleaf模板引擎的使用

Thymeleaf模板引擎的使用 一.模板引擎 JSP.Velocity.Freemarker.Thymeleaf 二.springboot推荐使用Thymeleaf模板引擎 特点:语法更简单,功能更强大: 1.引入Thymeleaf <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId

Thymeleaf模板引擎的初步使用

在springboot中,推荐使用的模板引擎是Thymeleaf模板引擎,它提供了完美的Spring MVC的支持.下面就简单的介绍一下Thymeleaf模板引擎的使用. 在controller层中,使用在类上使用@controller注解,注意的是,这里不是@restController注解,因为@restController注解是将结果作为json字符串进行返回的,并不是调用模板. 方法中,注入参数,Model model.然后同springmvc中的modelandview一样,model