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

【SpringBoot】 4 Thymeleaf

官网

https://www.thymeleaf.org/

介绍

Thymeleaf 是一个适用于 Web 和独立环境的现代服务器端 Java 模板引擎。
模板引擎:为了使用户界面和业务数据分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎会生成一个标准的 html 文档。
模板的作用:做好一个模板后,套入对应的位置的数据,最终以 html 的格式进行展示。
模板引擎的特点:提高页面、代码的复用性。

官网文档

https://www.thymeleaf.org/doc/tutorials/3.1/thymeleafspring.html

依赖

pom.xml

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

配置

thymeleaf 的默认配置,可以不改。

application.yml

spring:application:name: systemthymeleaf:prefix: classpath:/templates/ #前缀,默认为classpath:/templates/    suffix: .html #后缀,默认为.html

接口

在 controller 目录下,新建 UserController 类。
此处返回的是 user 页面。

UserController.java

package com.lm.system.controller;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;/*** @Author: DuHaoLin* @Date: 2024/7/26*/
@Controller
public class UserController {@GetMapping("user")public String user(Model model) {model.addAttribute("name", "Tom");model.addAttribute("age", 18);return "user";}}

返回页面

在 resource 目录下,新建 thymeleaf 默认获取的 templates 目录。
在 templates 目录下,新建 user.html 文件。

user.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Thymeleaf</title>
</head>
<body><span>姓名:</span><span th:text="${name}"></span><br /><span>年龄:</span><span th:text="${age}"></span>
</body>
</html>

效果图展示
在这里插入图片描述

项目目录结构

在这里插入图片描述

http://www.lryc.cn/news/407862.html

相关文章:

  • 动静资源的转发操作
  • Windows系统安全加固方案:快速上手系统加固指南(上)
  • git连接远程仓库
  • 算法-----递归~~搜索~~回溯(宏观认识)
  • 【云原生】Docker搭建知识库文档协作平台Confluence
  • 序列化与反序列化的本质
  • 飞牛爬虫FlyBullSpider 一款简单方便强大的爬虫,限时免费 特别适合小白!用它爬下Boss的2024年7月底Java岗位,分析一下程序员就业市场行情
  • EXCEL 排名(RANK,COUNTIFS)
  • 【踩坑系列-JS】iframe中的url参数获取
  • 测试工作中常听到的名词解释 : )
  • Linux内网离线用rsync和inotify-tools实现文件夹文件单向同步和双向同步
  • Spring Security学习笔记(二)Spring Security认证和鉴权
  • 产品经理NPDP好考吗?
  • 【C++】:红黑树的应用 --- 封装map和set
  • unity美术资源优化(资源冗余,主界面图集过多)
  • 【git】github中的Pull Request是什么
  • gitlab查询分支API显示不全,只有20个问题
  • vue3+vite 实现动态引入某个文件夹下的组件 - glob-import的使用
  • hhhhh
  • 扫雷小游戏纯后端版
  • RuoYi-Vue-Plus(动态添加移除数据源)
  • idea启动项目报:the command line via JAR manifest or via a classpath file and rerun.
  • vue3 + ts中有哪些类型是由vue3提供的?
  • 【Linux】远程连接Linux虚拟机(MobaXterm)
  • LeetCode Hot100 生成特殊数字的最少操作
  • Spring MVC 应用分层
  • QT--进程
  • 凸优化笔记-基本概念
  • 1858. 数组查找及替换
  • 计算机视觉与面部识别:技术、应用与未来发展