thymeleaf

1、引入場景html

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

2、SpringBoot 自動配置原理中的 thymeleaf 的自動配置規則java

只要咱們把 HTM L頁面放在 classpath:/templates/,thymeleaf 就能自動渲染;spring

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
  private static final Charset DEFAULT_ENCODING = Charset.forName("UTF‐8");
  private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");
  //前綴
  public static final String DEFAULT_PREFIX = "classpath:/templates/";
  //後綴
  public static final String DEFAULT_SUFFIX = ".html";

3、thymeleaf 使用express

一、導入 thymeleaf 的名稱空間(導入該名稱空間後,就會有 thymeleaf 語法提示了)api

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

二、helloworldsession

java 代碼
@Controller
public class HelloController {

    @RequestMapping("/success")
    public String success(Model model){
        model.addAttribute("msg", "你好");
        return "success";
    }
    
}

html 代碼
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!--th:text 將div裏面的文本內容設置爲 ${msg}-->
    <div th:text="${msg}"></div>

</body>
</html>
View Code

4、thymeleaf  語法規則app

一、th:text;改變當前元素裏面的文本內容;
th:任意html屬性;來替換原生屬性的值ide

二、 thymeleaf 支持的表達式spring-boot

${...}:獲取變量值;OGNL;
1)、獲取對象的屬性、調用方法
2)、使用內置的基本對象:例如:${session.foo}
  #ctx : the context object.
  #vars: the context variables.
  #locale : the context locale.
  #request : (only in Web Contexts) the HttpServletRequest object.
  #response : (only in Web Contexts) the HttpServletResponse object.
  #session : (only in Web Contexts) the HttpSession object.
  #servletContext : (only in Web Contexts) the ServletContext object.3)、內置的一些工具類對象  #execInfo : information about the template being processed.  #messages : methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{…} syntax.  #uris : methods for escaping parts of URLs/URIs  #conversions : methods for executing the configured conversion service (if any).  #dates : methods for java.util.Date objects: formatting, component extraction, etc.  #calendars : analogous to #dates , but for java.util.Calendar objects.  #numbers : methods for formatting numeric objects.  #strings : methods for String objects: contains, startsWith, prepending/appending, etc.  #objects : methods for objects in general.  #bools : methods for boolean evaluation.  #arrays : methods for arrays.  #lists : methods for lists.  #sets : methods for sets.  #maps : methods for maps.  #aggregates : methods for creating aggregates on arrays or collectio  #ids : methods for dealing with id attributes that might be repeated (for example, as a result of an iteration).4)、*{...}:選擇表達式:和${}在功能上是同樣;  補充:配合 th:object="${session.user}:    <div th:object="${session.user}">      <p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>      <p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>      <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>    </div>5)、  #{...}:獲取國際化內容  @{...}:定義URL;@{/order/process(execId=${execId},execType='FAST')}  : ~{...}:片斷引用表達式;<div th:insert="~{commons :: main}">...</div>6)、  Literals(字面量)  字符串  數字  Boolean  Null  字符串拼接; |The name is ${name}|  數學運算; + , ‐ , * , / , %
相關文章
相關標籤/搜索