Spring Boot学习,Web开发(二)

thymeleaf模板引擎

Thymeleaf是一个适用于Web和独立环境的现代服务器端Java模板引擎。Spring Boot 推荐使用的模板引擎就是Thymeleaf。

引入Thymeleaf

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Thymeleaf使用和语法

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
	private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;
	public static final String DEFAULT_PREFIX = "classpath:/templates/";
	public static final String DEFAULT_SUFFIX = ".html";
// 只要把html页面放在classpath:/templates/下,thymeleaf就会自动渲染

页面:
在这里插入图片描述
controlle:

@Controller
public class HelloController {
    @RequestMapping("/success")
    public String success(Map<String, Object> map) {
        map.put("hello", "你好,thymeleaf");
        return "success";
    }
}

在这里插入图片描述
语法请参照thymeleaf官网

想要在写thymeleaf表达式的时候有提示,就需要导入thymeleaf的名称空间

<!--xmlns:th="http://www.thymeleaf.org" 就是这一句导入thymeleaf的名称空间-->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>成功</title>
</head>
<body>
    <h1>成功</h1>
    <div th:text="${hello}"></div>
</body>
</html>

语法

可以使用th替换标签的任意属性。
在使用文档的第10章有

特性属性描述
Fragment inclusionth:insert 片段包含,插入,相当于jsp的include
th:replace 片段包含,替换
Fragment iteration th:each 遍历,相当于jsp的c:forEach
Conditional evaluationth:if判断,相当于jsp的c:if
th:unless
th:switch
th:case
Local variable definitionth:object/td> 声明变量
th:with
General attribute modificationth:attr修改任意属性
th:attrprepend在属性值前添加值
th:attrappend在属性值后添加值
Specific attribute modificationth:value特定属性的修改
th:href
th:src
...
Text (tag body modification)th:text修改标签体内容,不转义
th:utext修改标签体内容,转义
Fragment specificationth:fragment指定片段
Fragment removalth:remove移除片段
  • Simple expressions:(表达式语法)

    • Variable Expressions: ${…}
      • OGNL:
        • 获取对象的属性,调用方法
        • 使用内置对象
        • 内置的工具对象
    • Selection Variable Expressions: *{…}
      • 变量的选择表达式:和${}在功能上是一样的
        • 配合th:object="${session.user}"使用
    • Message Expressions: #{…}
      • 获取国际化内容的
    • Link URL Expressions: @{…}
      • 定义URL连接的
    • Fragment Expressions: ~{…}
      • 片段引用
  • Literals(字面量)

    • Text literals: ‘one text’ , ‘Another one!’ ,…
    • Number literals: 0 , 34 , 3.0 , 12.3 ,…
    • Boolean literals: true , false
    • Null literal: null
    • Literal tokens: one , sometext , main ,…
  • Text operations:(文本操作)

    • String concatenation: +
      • 字符串拼接
    • Literal substitutions: |The name is ${name}|
      • 字符串替换
  • Arithmetic operations:(数学运算)

    • Binary operators: + , - , * , / , %
    • Minus sign (unary operator): -
  • Boolean operations:(布尔运算)

    • Binary operators: and , or
    • Boolean negation (unary operator): ! , not
  • Comparisons and equality:(比较运算)

    • Comparators: > , < , >= , <= ( gt , lt , ge , le )
    • Equality operators: == , != ( eq , ne )
  • Conditional operators:(条件运算)

    • If-then: (if) ? (then)
    • If-then-else: (if) ? (then) : (else)
      • 三元运算
    • Default: (value) ?: (defaultvalue)
  • Special tokens:(特殊操作)

    • No-Operation: _
      • 没有操作:(不做任何操作)
        • (1 < 2) ? _ : false

示例:

@RequestMapping("/success")
    public String success(Map<String, Object> map) {
        map.put("hello", "你好,thymeleaf");
        map.put("h1", "<h1>你好啊!!</h1>");
        map.put("users", Arrays.asList("张三", "李四", "王五"));
        return "success";
    }
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>成功</title>
</head>
<body>
    <h1>成功</h1>
    <div th:text="${hello}"></div>
    <hr />
    <div th:text="${h1}"></div>
    <div th:utext="${h1}"></div>
    <hr />
    <h1 th:text="${user}" th:each="user: ${users}"></h1>
    <hr />
    <h2>
        <span th:each="user : ${users}">[[${user}]] </span>
    </h2>
</body>
</html>

结果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值