美文网首页中北软院创新实验室
Thymeleaf 语法 (持续更新)

Thymeleaf 语法 (持续更新)

作者: HikariCP | 来源:发表于2017-11-06 02:05 被阅读32次

简介

这里总结的是我自己在学习时用到的一些语法。由于是用到才会去查,因为太多我记不住。所以是记录式学习,会持续更新


标签

th:action

Form 表单
<form th:action="@{'/book/'+${book.id}}"></form> 

th:value

Input 标签
<input th:value="${book.id}"/>

th:href

a 标签
<a th:href="@{'/book/'+${book.id}}"></a>

th:each

table 表格
<tr th:each="book: ${books}"></tr>

th:text

table 表格
<td th:text="${book.id}"></td>

th:if

div 体展示
<div th:if="${books != null}"></div>

th:src

静态资源引入
<script th:src="@{/resources/js/jquery/jquery.json-2.4.min.js}"

th:object

表单数据对象的绑定

用于表单数据对象绑定,将表单绑定到后台controller的一个JavaBean参数。常与th:field一起使用进行表单数据绑定。

public class LoginBean implements Serializable{...}{}
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@ModelAttribute(value = "loginBean") LoginBean loginBean,ModelMap model){
        ...
    }
}
<form id="login-form" th:action="@{/login}" th:object="${loginBean}">...</form>

th:field

表单字段绑定,属性绑定、集合绑定。

注意操作符是 *

public class LoginBean implements Serializable {

    private String username;
    private List<User> user;

...}

---
public class User implements Serializable {

    private String username;;

}
---

@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@ModelAttribute(value = "loginBean") LoginBean loginBean,ModelMap model) {
  
}
<form id="login-form" th:action="@{/login}" th:object="${loginBean}">
    <input type="text" value="" th:field="*{username}"></input>
    <input type="text" value="" th:field="*{user[0].username}"></input>
</form>

th:id

div id声明
<div class="student" th:id = "stu+(${rowStat.index}+1)"></div>

th:fragment

声明定义该属性的div为模板片段。常用与头文件,页尾文件的引入。常与th:includeth:replace一起使用

  • 声明模板片段 /WEBINF/templates/footer. html
<div th: fragment="copy" >
    © 2011 The Good Thymes Virtual Grocery
</div>
  • 引入模板片段
<div th: include=" /templates/footer : : copy" ></div>
<div th: replace=" /templates/footer : : copy" ></div>

th:inline

[[…]]之间的表达式在 Thymeleaf 被认为是内联表达式,在其中您可以使用任何类型的表达式,也会有效th:text属性。

<p>Hello, [[${session.user.name}]]!</p>

等同于:

<p>Hello, <span th:text="${session.user.name}">Sebastian</span>!</p>

为了让内联工作,必须激活它使用th:inline 属性,它有三个可能的值或模式(text, javascriptnone)。

<p th:inline="text">Hello, [[${session.user.name}]]!</p>

如果不使用th:inline="text",则会被当做字符串显示。th:inline="javascript"表示能在 js 中使用[ [] ]取值。

参考:[thymeleaf中的内联 [ ] ]

相关文章

  • Thymeleaf 语法 (持续更新)

    简介 这里总结的是我自己在学习时用到的一些语法。由于是用到才会去查,因为太多我记不住。所以是记录式学习,会持续更新...

  • thymeleaf教程

    目录 thymeleaf的定义spring boot整合thymeleaf语法参考文档thymeleaf笔记thy...

  • 十、Thymeleaf使用以及语法

    SpringBoot推荐的Thymeleaf; 语法更简单,功能更强大; 1、引入thymeleaf; 2、Thy...

  • SpringBoot系列之集成Thymeleaf用法手册

    SpringBoot系列之Thymeleaf语法简单介绍 @[toc]Thymeleaf官方文档已经有比较详细的描...

  • Thymeleaf语法

    官方文档(基于3.0版本),基于springboot项目的,建议直接从第三章Using Texts开始查阅:htt...

  • vue语法(持续更新)

    1 语法 1.1 插值 数据绑定的形式:Mustache语法(双大括号)。 (1)文本插值 name:{{text...

  • 3.21学习

    Shiro(整合thymeleaf):认证和授权的框架Swagger(整合thymeleaf):API实时更新工具...

  • thymeleaf笔记

    1.thymeleaf特点:是通过特定语法对html的标记做渲染。 thymeleaf要求html的标记按照严谨的...

  • Thymeleaf

    Thymeleaf语法/标签说明语法/标签 说明layout:fragment 定义模板片段,可以在子页面用同...

  • springboot中避免Thymeleaf对html语法检查过

    最近在使用Thymeleaf的时候发现Thymeleaf对html语法的检查简直到了丧心病狂的地步,各种需要转义和...

网友评论

    本文标题:Thymeleaf 语法 (持续更新)

    本文链接:https://www.haomeiwen.com/subject/hpjkmxtx.html