当前位置: 首页 > news >正文

Spring Boot 集成 Thymeleaf​​ 的快速实现示例,无法渲染页面问题解决

以下是一个完整的 ​​Spring Boot 集成 Thymeleaf​​ 的快速实现示例,包含代码、配置和项目结构说明。所有步骤均基于最新实践整理,可直接用于开发。


1. ​​项目依赖(pom.xml)​

添加 Spring Boot Web 和 Thymeleaf 依赖:

<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><!-- 开发热更新(可选) --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope></dependency>
</dependencies>


2. ​​配置文件(application.yml)​

配置模板路径、缓存等(开发阶段建议关闭缓存):

spring:thymeleaf:prefix: classpath:/templates/  # 模板存放目录suffix: .html                  # 文件后缀cache: false                   # 禁用缓存(开发环境)encoding: UTF-8                # 编码mode: HTML                     # 模板模式


3. ​​项目结构​

 
src/
├── main/
│   ├── java/
│   │   └── com/example/demo/
│   │       ├── DemoApplication.java   # 启动类
│   │       └── controller/
│   │           └── HelloController.java  # 控制器
│   ├── resources/
│       ├── static/    # 静态资源(CSS/JS)
│       └── templates/ # Thymeleaf 模板
│           └── hello.html


4. ​​启动类(DemoApplication.java)​

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}

5. ​​控制器(HelloController.java)​

处理请求并传递数据到模板:

package com.example.demo.controller;
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("name", "Thymeleaf");model.addAttribute("users", new String[]{"Alice", "Bob", "Charlie"});return "hello"; // 对应 templates/hello.html}
}

重点、重点、重点:

注解用@Controller ,而不是@RestController

因为@RestController包括

@Controller
@ResponseBody

会导致,页面只显示"hello",不渲染页面。

API需要返回数据类容,API要加注解@ResponseBody


6. ​​Thymeleaf 模板(hello.html)​

使用 Thymeleaf 语法渲染动态内容:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>Thymeleaf Demo</title>
</head>
<body><h1 th:text="'Hello, ' + ${name} + '!'">默认文本(无数据时显示)</h1><!-- 条件判断 --><p th:if="${users.length > 0}">用户列表:</p><!-- 循环遍历 --><ul><li th:each="user : ${users}" th:text="${user}"></li></ul><!-- 表单示例 --><form th:action="@{/submit}" method="post"><input type="text" name="username" placeholder="输入用户名"><button type="submit">提交</button></form>
</body>
</html>


7. ​​运行与验证​

  1. 启动 DemoApplication 主类。
  2. 访问 http://localhost:8080/hello,页面将显示:
    • 标题:Hello, Thymeleaf!
    • 用户列表:Alice、Bob、Charlie
    • 表单输入框

8. ​​关键功能说明​

​功能​​实现方式​
​数据绑定​通过 model.addAttribute("key", value) 传递数据,模板中用 ${key} 引用 
​条件渲染​th:if / th:unless 控制元素显示 
​循环遍历​th:each="item : ${list}" 遍历集合 
​表单绑定​th:action="@{/url}" 和 th:field="*{fieldName}" 绑定表单数据 
​静态资源引用​使用 @{/path/to/resource} 引用 static/ 目录下的 CSS/JS 

扩展:表单提交处理

// 表单提交处理示例
@PostMapping("/submit")
public String submitForm(@RequestParam String username) {System.out.println("收到用户名:" + username);return "redirect:/success"; // 重定向到成功页
}
http://www.dtcms.com/a/268381.html

相关文章:

  • 记录一点开发技巧
  • Spring Boot 3.x 整合 Swagger(springdoc-openapi)实现接口文档
  • class类和style内联样式的绑定 + 事件处理 + uniapp创建自定义页面模板
  • React Ref 指南:原理、实现与实践
  • 深度学习篇---Yolov系列
  • 远程桌面启动工具
  • Flutter 每日翻译之 Widget
  • Day53GAN对抗生成网络思想
  • MySQL主从复制与读写分离概述
  • 一文了解PMI、CSPM、软考、、IPMA、PeopleCert和华为项目管理认证
  • Protein FID:AI蛋白质结构生成模型评估新指标
  • Redis-主从复制-分布式系统
  • 算法学习day15----蓝桥杯--进制转换
  • Web攻防-XMLXXE无回显带外SSRF元数据DTD实体OOB盲注文件拓展
  • 大数据Hadoop之——Flink1.17.0安装与使用(非常详细)
  • 桥梁桥拱巡检机器人cad+【4张】设计说明书+绛重+三维图
  • 了解微服务
  • JVM的内存区域划分,类加载器和GC
  • Modbus 与 BACnet 协议互操作:工业协议转换方案(一)
  • JavaSE -- 泛型详细介绍
  • 【机器学习笔记 Ⅱ】2 神经网络中的层
  • HCIA-生成数协议(STP)
  • Debezium日常分享系列之:Debezium管理平台
  • 【Elasticsearch入门到落地】15、DSL排序、分页及高亮
  • golang 协程 如何中断和恢复
  • WHAT - 依赖管理工具 CocoaPods
  • 从小白到进阶:解锁linux与c语言高级编程知识点嵌入式开发的任督二脉(1)
  • 如何确保Kafka集群的高可用?
  • 【MySQL】DTS机制对触发器时间的影响
  • Python-可视化学习笔记