Thymeleaf

作者: 華華公子 | 来源:发表于2020-02-20 10:21 被阅读0次

概述

Thymeleaf默认的页面文件是.html

  • maven依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  • Controller

返回字符串 index,对应模板文件index.html

@Controller
@RequestMapping(value = "/")
public class IndexController {

    @GetMapping(value = "/")
    public String index(Map<String,Object> paramMap) {
        paramMap.put("demo","hello world");
        return "index";
    }
}
  • Web页面

使用th:text获取传入参数

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>thymleaf demo</title>
</head>
<body>
<span th:text="${demo}"></span><br/>
</body>
</html>
  • href

直接从根路径开始

<a th:href="@{/channel/page/add}></a>"

动态参数

<a th:href="@{'/pageController?pageNum='+${pageNum}}" th:text="${pageNum}"></a>

可以使用附加语法来创建服务器根目录(而不是上下文相对)URL,以链
接到同一服务器中的不同上下文。 这些URL将被指定为

@{〜/path/to/something}

相关文章

网友评论

      本文标题:Thymeleaf

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