当前位置:网站首页>thymeleaf介绍与简单应用
thymeleaf介绍与简单应用
2022-07-16 11:17:00 【虎起来】
1.什么是thymeleaf
thymeleaf 跟 JSP 一样,就是运行之后,就得到纯 HTML了。 区别在与,不运行之前, thymeleaf 也是 纯 html …
所以 thymeleaf 不需要 服务端的支持,就能够被以 html 的方式打开,这样就方便前端人员独立设计与调试, jsp 就不行了, 不启动服务器 jsp 都没法运行出结果来。
2.在pom文件中添加对thymeleaf的支持
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3.增加控制器
package com.how2java.springboot.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(Model m) {
m.addAttribute("name", "thymeleaf");
return "hello";
}
}
4.新建hello.html
<!DOCTYPE HTML>
# 声明当前文件是 thymeleaf, 里面可以用th开头的属性
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
#name 的值显示在当前p里,取出来放进p里,取值用的是 "${name}" ,从而替换到 原来p 标签里的 4个字符 "name"
<p th:text="${name}" >name</p>
<p th:text="'Hello! ' + ${name} + '!'" >hello world</p>
<p th:text="|Hello! ${name}!|" >hello world</p>
</body>
</html>
这种方式,就可以把服务端的数据,显示在当前html里了。
5.application.properties配置
#thymeleaf 配置
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#缓存设置为false, 这样修改之后马上生效,便于调试
spring.thymeleaf.cache=false
#上下文
server.context-path=/thymeleaf
6.运行测试
边栏推荐
- Reverse salary increase, what is this operation?
- 1.8 类和对象
- 抢效率!上云规划时间缩短一半,这家企业的CTO做对了什么?
- 越来越难,不行就得多听几次
- 【华为联机对战】下载运行华为官方Unity示例代码,提示鉴权失败并返回错误码100114
- Using C language to realize the odd number problem of 0-10 -- Original
- Web APIs DOM event flow
- 1.6 method
- Mongodb slow query and index
- Analysis and summary of three technical solutions to realize app automation
猜你喜欢

【Harmony OS】【FAQ】鸿蒙应用开发问题分享(字体/构造器)

如何统计游戏中的数据

Implementation of distributed cronab based on cron library extension

Ceph分布式存储性能调优(六)

Oppo and Yijia will be banned in Germany? It turned out to be targeted by "patent hooligan" Nokia!

我发现了一款高效管理接口文档的神器

Cv2.resize pit

深度学习(DL, Deep Learning)入门1——LetNet5网络结构

Create and generate WiFi QR code mobile phone scanning link

健康防猝指南2:饮食健康
随机推荐
C语言位域
考研人暑期行为大赏top5
View for recording a temperature curve
链表 - 【删除所有重复的元素】
[golang learning notes] interface
Bill Gates is about to withdraw from the world's rich list by donating $20billion!
我发现了一款高效管理接口文档的神器
從 URL 輸入到頁面展現到底發生什麼?
Aardio - [library] graphical paging component library
深度学习(DL, Deep Learning)入门1——LetNet5网络结构
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
The position of limit n in PgSQL in SQL statement
【华为联机对战】下载运行华为官方Unity示例代码,提示鉴权失败并返回错误码100114
用c语言组合排列实现0-10的奇数问题——原创
微信小程序下拉刷新功能--onPullDownRefresh
数据湖基本架构
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
js中finally的使用
Error yolov5 PT to onnx error
Vscode 1.69 changes and concerns (three-way merge / terminal integration, etc.)