11 Simple Thymeleaf syntax

11.1 spring-boot environment preparation

         Important dependencies:

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

11.2 Forwarding messages without escaping

        That is, if a string is <h1>a title</h1>, it will be a string if it is not escaped; if it is escaped, it will output a title according to the syntax of html. The syntax of thymeleaf is: just add the th:text attribute to the tag, which is the non-escaping writing method. 

        Controller class: pass the name variable to the hello.html page.

package jiang.com.springbootstudy.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("/hello")
public class HelloWorldController {
    @RequestMapping("/test")
    public String hello(Model model){
        model.addAttribute("name","<h1>这是一个标题</h1>");
        return "hello";
    }
}

        HTML in the static folder:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:text="${name}"></div>
</body>
</html>

        Result after visiting the page:

 11.3 Forwarding message escaping

        Escaping means that when the forwarded string is written according to html rules, such as <h1>This is the title</h1>, the syntax output of html will be installed. Thymeleaf uses the th:utext attribute for escaping.

        The controller code is consistent with 11.2.

         HTML in the static folder:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div th:utext="${name}"></div>
</body>
</html>

          Result after visiting the page:

11.4 Traversing Arrays

        Controller class: use Array.asList() to convert the array into a collection;

package jiang.com.springbootstudy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;

@Controller
@RequestMapping("/hello")
public class HelloWorldController {
    @RequestMapping("/test")
    public String hello(Model model){
        model.addAttribute("names", Arrays.asList("zhangsan", "lisi")); // Arrays.asList把数组转为集合
        return "hello";
    }
}

        html: first use th:each = "name : ${names}" to traverse the names, and assign the value extracted each time to the name variable; then use th:text = "${name}" to extract the value of the name variable :

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div th:each= "names:${names}" th:text="${names}"></div>
</body>
</html>

        URL access result:

Guess you like

Origin blog.csdn.net/no996yes885/article/details/131879512