Thymeleaf

  • Post author:
  • Post category:其他


一、什么是Thymeleaf

Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎。

在应用开发中,可以使用Thymeleaf来完全代替JSP或其他模板引擎,它的主要作用是在静态页面上渲染显示动态数据。

Thtmeleaf的特点是动静结合,开箱即用,多方言支持,与springBoot完美整合等等。

二、Thtmeleaf绑定动态数据的三种方式

1.第一种绑定方式


//    第一种绑定方式
    @RequestMapping("index")
    public String index(HttpServletRequest request){
        request.setAttribute("hello","baby");
        request.setAttribute("content","这桌菜可真好恰!");
        return "/index.html";
    }

2.第二种绑定方式

//    第二种绑定方式
    @RequestMapping("index2")
    public String index2(Model model){
        model.addAttribute("hi","唐宝器");
        return "/index.html";
    }

3.第三种绑定方式

 //    第三种绑定方式
    @RequestMapping("index3")
    public String index3(Map map){
        map.put("title","秦淮河边舞!");
        return "/index.html";
    }

三、SpringBoot整合Thymeleaf

1.新建项目,在pom.xml中导入依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/>
</parent>


 <dependencies>
        <!--        添加web启动器坐标-->
        <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>

        <!--        SpringBoot集成Mybatis-->
        <!--        mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!--        分页-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.12</version>
        </dependency>
        <!--        mysql数据库-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.23</version>
        </dependency>
        <!--        数据连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.21</version>
        </dependency>

        <!--        测试依赖-->
        <!--junit依赖-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--springBoot测试框架-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--        spring开发工具-热部署依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

2.在resource目录下创建application.yml配置

spring:
  thymeleaf:
    enabled: true #开启thymeleaf视图解析
    encoding: utf-8 #编码
    prefix: classpath:/static/ #thymeleaf翻译页面路径
    cache: false #是否使用缓存
    mode: HTML #严格的HTML语法模式
    suffix: html #后缀名

  datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/demo3?characterEncoding=utf-8
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
    maxActive: 50
    maxWait: 2000
    minIdle: 10

mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  type-aliases-package: com.project.bean

pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

server:
  port: 8099 #修改端口号

3.书写启动类MainServer

@SpringBootApplication
@MapperScan("com.project.dao")
public class MainServer {
    public static void main(String[] args) {
        SpringApplication.run(MainServer.class,args);
    }
}

4.在resources中创建static目录,在static目录创建 index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--th:text为Thymeleaf属性,用于在展示文本-->
<h1 th:text="小鸡炖蘑菇">天王盖地虎</h1>
<h1 th:text="${hello}">天王盖地虎</h1>

<span th:text="${content}"></span>
<span th:text="${hi}"></span>
<span th:text="${title}"></span>



</body>
</html>

5.控制层,创建TestController用于测试

@Controller
public class ThymeleafController {

    @RequestMapping("index")
    public String index(HttpServletRequest request){
        request.setAttribute("hello","baby");
        request.setAttribute("content","这桌菜可真好恰!");
        return "/index.html";
    }


    @RequestMapping("index2")
    public String index2(Model model){
        model.addAttribute("hi","唐宝器");
        return "/index.html";
    }

}



版权声明:本文为as15282235592原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。