Springboot整合thymeleaf

本文介绍如何在SpringBoot项目中集成Thymeleaf模板引擎,包括创建项目、添加依赖、配置缓存、编写HTML及控制器代码,实现前后端数据交互。

简介

使用springboot 来集成使用Thymeleaf可以大大减少单纯使用thymleaf的代码量,所以我们接下来使用springboot集成使用thymeleaf.

实现的步骤为:

创建一个sprinboot项目
添加thymeleaf的起步依赖
添加spring web的起步依赖
编写html,使用thymleaf的语法获取变量对应后台传递的值
编写controller ,设置变量的值到model中

整合

(1)创建工程

创建一个独立的工程springboot-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:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mytest</groupId>
    <artifactId>springboot-thymeleaf</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </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>
    </dependencies>
</project

(2)创建html

在resources中创建templates目录,在templates目录创建 demo1.html,代码如下:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Thymeleaf的入门</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<!--输出hello数据-->
<p th:text="${hello}"></p>
</body>
</html>

(3)修改application.yml配置

创建application.yml,并设置thymeleaf的缓存设置,设置为false。默认加缓存的,用于测试。

spring:
  thymeleaf:
    cache: false

(4)控制层

创建controller用于测试后台 设置数据到model中。

创建com.mytest.controller.TestController,代码如下:

@Controller
@RequestMapping("/test")
public class TestController {

    /***
     * 访问/test/hello  跳转到demo1页面
     * @param model
     * @return
     */
    @RequestMapping("/hello")
    public String hello(Model model){
        model.addAttribute("hello","hello welcome");
        return "demo1";
    }
}

(5)启动系统,并在浏览器访问

http://localhost:8080/test/hello

在这里插入图片描述

### 集成ThymeleafSpring Boot项目 为了在Spring Boot应用程序中集成Thymeleef模板引擎,仅需引入`spring-boot-starter-thymeleaf`依赖即可[^1]。当此依赖被加入至项目的构建文件(如Maven的pom.xml),Spring Boot自动配置机制将会识别并设置好必要的组件来支持Thymeleaf作为视图解析器[^4]。 #### Maven依赖声明 对于采用Maven管理依赖关系的应用程序来说,在`pom.xml`内添加如下片段: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 一旦完成上述操作,则无需额外的手动配置;因为Spring Boot的设计理念就是减少开发者的工作量,使得创建独立运行、生产级别的基于Spring框架的新一代应用变得简单快捷[^2]。 #### 创建控制器类 接下来定义一个简单的控制器用于处理HTTP请求并将数据传递给Thymeleaf模板渲染页面输出。下面是一个基本的例子展示如何实现这一点: ```java import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HelloController { @GetMapping("/hello") public String hello(Model model) { model.addAttribute("message", "Hello from Thymeleaf!"); return "hello"; } } ``` 这段代码中的`@Controller`注解表明这是一个Spring MVC控制器,而`@GetMapping("/hello")`则指定了该方法响应来自路径`/hello`的GET请求。最后通过返回字符串`"hello"`告诉Spring去查找名为`hello.html`的Thymeleaf模板文件来进行视图呈现[^5]。 #### 编写Thymeleaf模板 最后一步是在资源目录下的适当位置放置HTML文件——通常位于`src/main/resources/templates/`下,并命名为`hello.html`。这里给出一段非常基础的HTML文档结构加上一点Thymeleaf语法用来显示之前由Java端传过来的消息属性: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Hello Page</title> </head> <body> <h1 th:text="${message}">Placeholder text here...</h1> </body> </html> ``` 以上便是整个过程概述以及具体实施步骤说明。值得注意的是即使不使用Spring也可以单独利用Thymeleaf进行Web开发工作[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值