码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • [Spring]Thymeleaf——XML/XHTML/HTML5模板引擎


    什么是Thymeleaf?

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

    Thymeleaf is a modern server-side Java template engine for both web and standalone environments.
    官方文档


    使用

    Maven依赖

    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-thymeleafartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    application.yml配置

    • cache : 设置thymeleaf的缓存。默认true,用于测试。
    • encoding : 编码
    • prefix : classpath:/templates/,模板文件路径
    spring:
      thymeleaf:
        cache: false
        encoding: UTF-8
    
    • 1
    • 2
    • 3
    • 4

    创建html

    在resources/templates/目录中创建xxx.html。
    这里创建了hello.html。

    DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>titletitle>
    head>
    <body>
    	
    	<div th:text="${key}">div>
    body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    Controller返回页面

    @Controller
    @RequestMapping("/test")
    public class TestController {
    
        /**
        * 访问test/hello,返回hello.html
        */
        @RequestMapping("/hello")
        public String hello(Model model){
            model.addAttribute("key","hello world");
            return "hello";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    常用标签

    标签作用
    th:id替换id
    th:text文本替换
    th:utext支持html的文本替换
    th:object替换对象
    th:value替换值
    th:each迭代
    th:href替换超链接
    th:src替换资源

    取值

    ${Key}

    循环
    <ul th:each="item:${MyList}">
        <li th:text="${item}">li>
    ul>
    
    • 1
    • 2
    • 3

    引入资源

    @{资源地址}

    读取配置文件中数据

    #{key}

    引用公共页面

    • th:insert:将代码块片段整个插入到使用了 th:insert 属性的 HTML 标签中;
    • th:replace:将代码块片段整个替换使用了 th:replace 属性的 HTML 标签中;
    • th:include:将代码块片段包含的内容插入到使用了 th:include 属性的 HTML 标签中。
  • 相关阅读:
    浅析Java设计模式【3.3】——观察者
    Oracel增加IP白名单限制
    ElasticSearch7.3学习(十四)----生产环境实时重建索引
    python基础之函数__name__属性
    安装篇(整理了各大神软件安装教程集合)
    pandas使用fillna函数填充dataframe中所有数据列的缺失值、使用中位数(median)填充dataframe所有数据列中的缺失值
    【Shell牛客刷题系列】SHELL20 打印只有一个数字的行:awk叫上正则表达式好兄弟来刷题~
    【剑指offer系列】37. 序列化二叉树
    Wi-Fi7将带来前所未有的快捷、稳定的互联网,更快的传输速度
    react15升级17问题记录
  • 原文地址:https://blog.csdn.net/malu_record/article/details/133819679
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号