Use of Thymeleaf for Java Learning

Foreword:

For the follow-up code auditing, it is necessary to understand some commonly used frameworks and technologies, and regain the development knowledge content such as Spring Boot here.

1 |20x01 Introduction to Thymeleaf

 

Thymeleaf is a modern server-side Java template engine web and independent environment, capable of processing HTML, XML, JavaScript, CSS, and even plain text.

The main goal of Thymeleaf is to provide an elegant and highly maintainable way to create templates. To achieve this, it is built on the concept of a natural template, and its logic is injected into the template file in a way that does not affect the use of the template as a design prototype. This improves design communication and bridges the gap between design and development teams.

Thymeleaf’s design has taken web standards into consideration from the very beginning, especially HTML5

Thymeleaf is a very extensible template engine (in fact it can be called a template engine framework), which allows you to define and customize the way your templates will be processed to a fine level of detail.

Objects that apply some logic to mark artifacts (marks, some text, comments, or placeholders if the template is not a mark) are called handlers, and a collection of these handlers—plus some additional artifacts—usually in dialects Part. Thymeleaf's core library provides a dialect called the standard dialect, which should be sufficient for most users.

1 |30x02 Thymeleaf basic configuration

 

Here is mainly Srping Boot

<!--引入thymeleaf依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

Create an HTML file in the resources\templates directory of the project. Here, pay attention to importing the thymeleaf namespace, otherwise the template cannot be rendered.

<!doctype html>

<!--注意:引入thymeleaf的名称空间-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <p th:text="'hello SpringBoot'">hello thymeleaf</p>
</body>
</html>

Write Controller

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class IndexController {

    @GetMapping("home")
    public String index() {
        return "index";
    }
}

The annotation here needs to use @Controller, not @RestController annotation, otherwise an error will be reported.

  1. If you just use @RestController to annotate the Controller, the method in the Controller cannot return the jsp page or html, the configured view resolver InternalResourceViewResolver does not work, and the content returned is the content in Return.
  2. If you need to return to the specified page, you need to use @Controller with the view resolver InternalResourceViewResolver.
    If you need to return JSON, XML or custom mediaType content to the page, you need to add @ResponseBody annotations to the corresponding method.

1 |40x03 Thymeleaf syntax

 

Types of

  • 1. Variable expression
  • 2. Choice or asterisk expression
  • 3. Text internationalization expression
  • 4. URL expression

${...} variable expression

<span th:text="${book.author.name}">  
<li th:each="book : ${books}">  

@{...} link expression

 @{/order/list} 
 @{/order/details(id=${orderId})}  

或者是
    
<form th:action="@{/createOrder}">  
<a href="main.html" th:href="@{/main}">

#{...} Message expression

#{main.title}  
#{message.entrycreated(${entryId})}  

*{...} select variable expression

<div th:object="${book}">  
...  
<span th:text="*{title}">...</span>  
...  
</div> 

Commonly used th tags

th tag attribute

1) th:text: text replacement;

2) th:utext: Support html text replacement.

3) th:value: attribute assignment

4) th:each: traverse loop elements

5) th:if: judgment conditions, similar to th:unless, th:switch, th:case

6) th:insert: code block introduction, similar to th:replace, th:include, often used in common code block extraction scenarios

7) th:fragment: defines the code block to be easily referenced by th:insert

8) th:object: declare variables, generally used in conjunction with *{} to achieve the effect of laziness.

9) th:attr: set tag attributes, multiple attributes can be separated by commas

Use of Thymeleaf for Java Learning

 

Use of Thymeleaf for Java Learning

 

1|50x04 end

The content is relatively simple, mainly as a record.

Guess you like

Origin blog.csdn.net/a159357445566/article/details/113704149