• Thymeleaf学习(1)—— 表达式和属性


    一、初始化设置 

    添加依赖

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-thymeleafartifactId>
    4. dependency>

    在html中需要添加以下内容

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

    可以在下面添加如下内容,可以关闭thymeleaf的红线报错

    在application.properties文件中可以添加以下内容,也可以不添加(都是默认的)

    1. #### 以下内容都为默认,可以不用添加 #####
    2. #在开发阶段,关闭模板引擎
    3. spring.thymeleaf.cache=false
    4. #编码格式
    5. spring.thymeleaf.encoding=UTF-8
    6. #模板的类型(默认是HTML)
    7. spring.thymeleaf.mode=HTML
    8. #模板前缀:文件的路径
    9. spring.thymeleaf.prefix=classpath:/templates/
    10. #模板后缀
    11. spring.thymeleaf.suffix=.html

    二、表达式

    1. 标准变量表达式:${key}

    语法:${key},在页面的标签中使用 th:text="${key}"

    作用:获取request作用域中key对应的值。可以使用request.setAttribute(key, value)或者model.addAttribute(key, value)往request作用域中存放数据。

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <p th:text="${data}">数据p>
    9. <p th:text="${user}">用户p>
    10. <p th:text="${user.id}">idp>
    11. <p th:text="${user.name}">姓名p>
    12. <p th:text="${user.age}">年龄p>
    13. body>
    14. html>

    如果直接进入test.html页面,则会显示原来标签中间的数据,只有通过controller才能获取到作用域中的值

    1. package com.xdu.studyspringboot.controller;
    2. import com.xdu.studyspringboot.pojo.User;
    3. import org.springframework.stereotype.Controller;
    4. import org.springframework.ui.Model;
    5. import org.springframework.web.bind.annotation.RequestMapping;
    6. import javax.servlet.http.HttpServletRequest;
    7. @Controller
    8. public class ThymeleafController {
    9. @RequestMapping("/testThymeleaf")
    10. public String testThymeleaf(HttpServletRequest request, Model model){
    11. request.setAttribute("data", "Thymeleaf模板引擎");
    12. model.addAttribute("user", new User(1, "Tom", 23)); //User是一个实体对象的类
    13. return "test";
    14. }
    15. }

    2. 选择变量表达式:*{key}

    语法:*{key},在标签中使用 th:text="*{key}"

    作用:获取request作用域中key对应的值。可以和th:object一起使用更方便获取对象的属性值

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <p th:text="*{user.name}">p>
    9. <p th:text="*{data}">p>
    10. <div th:object="${user}">
    11. <p th:text="*{id}">p>
    12. <p th:text="*{name}">p>
    13. <p th:text="*{age}">p>
    14. div>
    15. body>
    16. html>
    1. package com.xdu.studyspringboot.controller;
    2. import com.xdu.studyspringboot.pojo.User;
    3. import org.springframework.stereotype.Controller;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import javax.servlet.http.HttpServletRequest;
    6. @Controller
    7. public class ThymeleafController {
    8. @RequestMapping("/testThymeleaf")
    9. public String testThymeleaf(HttpServletRequest request){
    10. request.setAttribute("data","Thymeleaf");
    11. request.setAttribute("user", new User(1, "Tom", 23)); //User是一个实体对象的类
    12. return "test";
    13. }
    14. }

    3. 链接表达式:@{url}

    语法:@{url},在标签中使用

    作用:链接到其他地址

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>index页面title>
    6. head>
    7. <body>
    8. <h1>链接到绝对地址h1>
    9. <a th:href="@{http://www.baidu.com}">百度a> <br/>
    10. <h1>链接到相对地址,没有参数h1>
    11. <a th:href="@{/test}">test.html页面a> <br/>
    12. <a th:href="@{/testThymeleaf}">通过访问controller来进入test.html页面a> <br/>
    13. <h1>链接到相对地址,有参数h1>
    14. <a th:href="@{/queryUser?id=1}">链接到有参数的页面a> <br/>
    15. <a th:href="@{/queryUser(id=2)}">链接到有参数的页面a> <br/>
    16. body>
    17. html>
    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <h1>test.html页面h1>
    9. <p th:text="${data}">数据p>
    10. <p th:text="${user}">用户p>
    11. body>
    12. html>
    1. package com.xdu.studyspringboot.controller;
    2. import com.xdu.studyspringboot.pojo.User;
    3. import org.springframework.stereotype.Controller;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.ResponseBody;
    6. import javax.servlet.http.HttpServletRequest;
    7. @Controller
    8. public class ThymeleafController {
    9. @RequestMapping("/")
    10. public String index(){
    11. return "index";
    12. }
    13. @RequestMapping("/test")
    14. public String testPage(){
    15. return "test";
    16. }
    17. @RequestMapping("/testThymeleaf")
    18. public String testThymeleaf(HttpServletRequest request){
    19. request.setAttribute("data","Thymeleaf");
    20. request.setAttribute("user", new User(1, "Tom", 23)); //User是一个实体对象的类
    21. return "test";
    22. }
    23. @RequestMapping("/queryUser")
    24. @ResponseBody
    25. public String queryUser(Integer id){
    26. return "参数:"+id;
    27. }
    28. }

    三. 属性 

    Thymeleaf的属性大多都是:html属性加上th前缀,作用和原本的html属性作用相同,如:th:action="",th:text="",th:href="",th:src="",th:method=""等

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <form th:action="@{/login}" th:method="post">
    9. 姓名:<input th:type="text" th:name="name"> <br/>
    10. <input type = "submit" value = "注册"/>
    11. form>
    12. <p th:text="${name}">p>
    13. body>
    14. html>
    1. package com.xdu.studyspringboot.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import javax.servlet.http.HttpServletRequest;
    5. @Controller
    6. public class ThymeleafController {
    7. @RequestMapping("/login")
    8. public String testLogin(HttpServletRequest request){
    9. String username = request.getParameter("name");
    10. request.setAttribute("name", username);
    11. return "test";
    12. }
    13. }

    四. 内联文本

    内联文本th:inline,可以不依赖html的标签直接获取值,方式是:[[${key}]]

     也可以在js中使用内联文本获取值

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. <script type="text/javascript" th:inline="javascript">
    7. var name = [[${name}]];
    8. var age = [[${age}]];
    9. alert("我是"+name+",年龄"+age);
    10. script>
    11. head>
    12. <body>
    13. <p>我是[[${name}]],年龄[[${age}]]p> <br/>
    14. <p>我是[[${user.name}]],年龄[[${user.age}]]p> <br/>
    15. body>
    16. html>
    1. package com.xdu.studyspringboot.controller;
    2. import com.xdu.studyspringboot.pojo.User;
    3. import org.springframework.stereotype.Controller;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import javax.servlet.http.HttpServletRequest;
    6. @Controller
    7. public class ThymeleafController {
    8. @RequestMapping("/inline")
    9. public String testInline(HttpServletRequest request){
    10. request.setAttribute("name","Tom");
    11. request.setAttribute("age",22);
    12. request.setAttribute("user", new User(1, "Tom", 22));
    13. return "test";
    14. }
    15. }

    五. 字符串连接

     1. 使用单引号

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <p th:text="'我是'+${name}+',年龄'+${age}">p>
    9. body>
    10. html>

    2. 使用双竖线

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <p th:text="|我是${name},年龄${age}|">p>
    9. body>
    10. html>
    1. package com.xdu.studyspringboot.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import javax.servlet.http.HttpServletRequest;
    5. @Controller
    6. public class ThymeleafController {
    7. @RequestMapping("/stringLink")
    8. public String testStringLink(HttpServletRequest request){
    9. request.setAttribute("name","Tom");
    10. request.setAttribute("age",22);
    11. return "test";
    12. }
    13. }
  • 相关阅读:
    K8S:K8S自动化运维容器Docker集群
    SpringMVC(第一个项目HelloWorld))
    Spring-aop技术
    系统架构设计师-第9章-软件可靠性基础知识-学习笔记
    python type hint
    【JVM面试】从JDK7 到 JDK8, JVM为啥用元空间替换永久代?
    注解(Annotation)基础
    web前端期末大作业【仿12306铁路官网首页】学生网页设计作业源码
    Vue2使用dhtmlx-gantt插件实现复杂甘特图
    TMS320F28374S之CMPSS
  • 原文地址:https://blog.csdn.net/Archer__13/article/details/126894807