SpringBoot学习-part21 tymeleaf 了解基本语法与小练习

找到Thymeleaf配置文件

在这里插入图片描述在这里插入图片描述

将Html页面放到classpath:/templates/

classpath:/templates/

在这里插入图片描述

Thymeleaf按照如下方式寻找资源和拼串儿
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

在html中取出值

1.页面中导入Thymeleaf名称空间

<html lang="en" xmlns:th="http://www.thymeleaf.org">

在这里插入图片描述
2.利用 th:text 设置div的文本值
在这里插入图片描述在这里插入图片描述

thymeleaf 众多属性

在这里插入图片描述

Thymeleaf基本表达式

Variable Expressions: ${…}

// 获取变量值 OGNL

  1. 获取对象属性,甚至调用方法
    在这里插入图片描述
  2. 使用内置的基本对象:
#ctx : the context object.
#vars: the context variables.
#locale : the context locale.
#request : (only in Web Contexts) the HttpServletRequest object.
#response : (only in Web Contexts) the HttpServletResponse object.
#session : (only in Web Contexts) the HttpSession object.
#servletContext : (only in Web Contexts) the ServletContext object
  1. 内置的工具对象
#execInfo : information about the template being processed.
#messages : methods for obtaining externalized messages inside variables expressions, in the same way as they
would be obtained using #{…} syntax.
#uris : methods for escaping parts of URLs/URIs
#conversions : methods for executing the configured conversion service (if any).
#dates : methods for java.util.Date objects: formatting, component extraction, etc.
#calendars : analogous to #dates , but for java.util.Calendar objects.
#numbers : methods for formatting numeric objects.
#strings : methods for String objects: contains, startsWith, prepending/appending, etc.
#objects : methods for objects in general.
#bools : methods for boolean evaluation.
#arrays : methods for arrays.
#lists : methods for lists.
#sets : methods for sets.
#maps : methods for maps.
#aggregates : methods for creating aggregates on arrays or collections.
#ids : methods for dealing with id attributes that might be repeated (for example, as a result of an iteration).

Selection Variable Expressions: *{…}

用th:object 将 user 包装起来,后面的 * 表示该 object
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

Message Expressions: #{…}

在这里插入图片描述

#messages : methods for obtaining externalized messages inside variables expressions, in the same way as they
would be obtained using #{…} syntax.

Link URL Expressions: @{…}

注意这里是如何传递参数的
在这里插入图片描述
多个参数可以使用逗号分隔
在这里插入图片描述
Fragment Expressions: ~{…}

类似于引入命名空间内的方法或变量

在这里插入图片描述

其它表达式

literals字面值

在这里插入图片描述
在这里插入图片描述追加文本

在这里插入图片描述

Arithmetic operations数学运算

在这里插入图片描述

Expression inlining 行内表达式

在这里插入图片描述

练习测试

- Controller

@Controller
public class HelloController {

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "helloworld";
    }

    @RequestMapping("/test")
    public String success(Map<String,Object> map){
        map.put("hello","<h1>你好呀</h1>");
        map.put("users", Arrays.asList("Tom","Jerry","Mickey"));

        return "success";
    }
}

- HTML

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>Success</h1>
    <div th:text="${hello}"></div>
    <!-- utext 不转译 -->
    <div th:utext="${hello}"></div>
    <!-- th:each每次遍历都会生成这个标签 -->
    <h4 th:each="user:${users}" th:text="${user}"></h4>
    <hr/>
    <h4>
        <!-- 一个h4中包含多个span -->
        <span th:each="user:${users}">[[${user}]]</span>
    </h4>
</body>
</html>

- 输出

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值