ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Thymeleaf模板入门(二)

2021-04-01 19:01:35  阅读:169  来源: 互联网

标签:片段 入门 标签 Thymeleaf 模板 th public 表达式 变量


Thymeleaf基本语法

1. 前篇

Thymeleaf模板入门(一)

2. 常用标签

th:标签说明
th:insert页面片段包含
th:replace页面片段包含
th:each元素遍历
th:if条件判断,条件成立时显示th标签内容
th:unless条件判断,条件不满足时显示
th:switch条件判断,进行选择时判断
th:caseth:switch分支的条件判断
th:object用于替换对象
th:with用于定义局部变量
th:attr通用属性修改
th:attrprepend通用属性修改,将计算结果追加前缀到现有属性值
th:attrappend通用属性修改,将计算结果追加后缀到现有属性值
th:value属性值修改,指定标签属性值
th:href用于设定链接地址
th:src用于设定链接地址
th:text用于指定标签显示的文本内容
th:utext用于指定标签显示的文本内容,对特殊标签不转义
th:fragment声明片段
th:remove移除片段

3. 主要标准表达式

语法说明
${…}变量表达式
*{…}选择变量表达式
#{…}消息表达式
@{…}链接URL表达式
~{…}片段表达式

3.1 变量表达式

<p th:text="${title}">变量表达式</p>

${…}主要获取上下文中变量值,若程序未启动或者上下文不存在时显示标签内的内容。

内置对象:

  • #ctx:上下文对象
  • #vars:上下文变量
  • #locale:上下文区域设置
  • #request:(仅限Web Context)HttpServletRequest对象
  • #response:(仅限Web Context)HttpServletServlet对象
  • #session:(仅限Web Context)HttpSession对象
  • #servletContext:(仅限Web Context)ServletContext对象

例:获取国家信息

<span th:text="${#locale.country}">CN</span>

3.2 选择变量表达式

选择变量表达式一般用于从被选定对象而不是上下文中获取属性值,若没有选定对象,则和变量表达式一样

<h2>选择变量表达式</h2>
<dev th:object="${user}">
    <span th:text="*{name}"></span>
    <span th:text="*{age}"></span>
</dev>

3.3 消息表达式

消息表达式主要用于Thymeleaf模板从消息源中提取消息内容实现国际化

<title th:text="#{user.title}"></title>

3.4 链接URL表达式

一般用于页面跳转和资源引入

<a th:href="@{http://infinxkj.top}">让大古编程</a>

3.5 片段表达式

将标记片段移动到模板中的方法

<div th:insert="~{demo::hello}">让大古编程</div>

模板demo.html

<template xmlns:th="http://www.thymeleaf.org">
    <div th:fragment="hello">
        <a th:href="@{http://infinxkj.top}">让大古编程</a>
    </div>
</template>

3.6

hello.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
</head>
<body>
<h1>这是一个测试页面</h1>
<h3>变量表达式</h3>
<p th:text="${user.name}">变量表达式</p>
<h3>选择变量表达式</h3>
<dev th:object="${user}">
    <span th:text="*{name}">姓名</span>
    <span th:text="*{age}">年龄</span>
</dev>
<h3>消息表达式</h3>
<h3>链接URL表达式</h3>
<a th:href="@{http://infinxkj.top}">让大古编程</a>
<h3>片段表达式</h3>
<div th:insert="~{demo::hello}">让大古编程</div>
</body>
</html>

demo.html

<template xmlns:th="http://www.thymeleaf.org">
    <div th:fragment="hello">
        <a th:href="@{http://infinxkj.top}">让大古编程</a>
    </div>
</template>

DemoController.java

package top.infinxkj.thymeleaf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import top.infinxkj.thymeleaf.pojo.User;

@Controller
public class DemoController {
    @GetMapping
    public String hello(Model model){
        User user = new User();
        user.setName("张三");
        user.setAge(28);
        model.addAttribute("user",user);
        return "hello";
    }
}

User.java

package top.infinxkj.thymeleaf.pojo;

public class User {
    public String name;
    public int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

启动器

package top.infinxkj.thymeleaf;

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);
    }
}

效果

在这里插入图片描述

标签:片段,入门,标签,Thymeleaf,模板,th,public,表达式,变量
来源: https://blog.csdn.net/m0_45086848/article/details/115361838

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有