Spring Boot used thymeleaf

Spring Boot support for FreeMarker, Groovy, Thymeleaf and Mustache four kinds of template parsing engine, official recommended Thymeleaf.

spring-boot-starter-thymeleaf

Use Thymeleaf just add Thymeleaf in the pom to the starter in Spring Boot in:

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

 

In Spring Boot 1.5.9.RELEASE version, the default version 2.1.6.RELEASE Thymeleaf version 3.0 or later is recommended. Version in the pom Thymeleaf revised to 3.0.2.RELEASE:

<properties>
  <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
  <thymeleaf-layout-dialect.version>2.0.1</thymeleaf-layout-dialect.version>
</properties>

 

In Spring Boot, the default html page address is src / main / resources / templates, a default static resources to address src / main / resources / static.

Thymeleaf default configuration

You can modify the default configuration Thymeleaf in Spring Boot configuration file:

# Enable template cache (default value: to true) 
spring.thymeleaf.cache = to true
#check that the before at The Template Rendering IT EXISTS.
Spring.thymeleaf.check to true-Template =
# to check the correct position of the template (default: to true)
the Spring. Template-LOCATION =-thymeleaf.check to true
# Content-Type value (default value: text / HTML)
spring.thymeleaf.content-of the type = text / HTML
# open MVC Thymeleaf view resolution (default value: to true)
spring.thymeleaf. to true = Enabled
# template encodes
spring.thymeleaf.encoding. 8 = UTF-
# to be excluded is outside the view list name parsing, separated by commas
spring.thymeleaf.excluded-view-names =
# to be used on the template template mode. See StandardTemplate-ModeHandlers (default value: the HTML5)
spring.thymeleaf.mode the HTML5 =
# is added to the prefix (default: classpath: / templates /) front view name in the construction of the URL
spring.thymeleaf.prefix CLASSPATH =: / Templates /
# Added to the suffix (default: .html) after the name of the view in the construction of the URL
spring.thymeleaf.suffix = .html
order #Thymeleaf template parser parser chain. By default, it ranked first. 1 in order from the beginning, only defines additional TemplateResolver Bean only need to set this property.
spring.thymeleaf.template-resolver-order =
name list view # resolvable, separated by commas
spring.thymeleaf.view-names =

 

General will develop spring.thymeleaf.cache set to false, the other can keep the default value.

A simple example

Write a simple Controller:

@Controller
public class IndexController {

  @RequestMapping("/account")
  public String index(Model m) {
      List<Account> list = new ArrayList<Account>();
      list.add(new Account("KangKang", "康康", "e10adc3949ba59abbe56e", "超级管理员", "17777777777"));
      list.add(new Account("Mike", "麦克", "e10adc3949ba59abbe56e", "管理员", "13444444444"));
      list.add(new Account("Jane","简","e10adc3949ba59abbe56e","运维人员","18666666666"));
      list.add(new Account("Maria", "玛利亚", "e10adc3949ba59abbe56e", "清算人员", "19999999999"));
      m.addAttribute("accountList",list);
      return "account";
  }
}

 

Write account.html page:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <title>account</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <link rel="stylesheet" th:href="@{/css/style.css}" type="text/css">
</head>
<body>
  <table>
      <tr>
          <th>no</th>
          <th>account</th>
          <th>name</th>
          <th>password</th>
          <th>accountType</th>
          <th>tel</th>
      </tr>
      <tr th:each="list,stat : ${accountList}">
          <td th:text="${stat.count}"></td>
          <td th:text="${list.account}"></td>
          <td th:text="${list.name}"></td>
          <td th:text="${list.password}"></td>
          <td th:text="${list.accountType}"></td>
          <td th:text="${list.tel}"></td>
      </tr>
  </table>
</body>
</html>

 

The final project directory as follows:

Start the project, visit HTTP: // localhost: 8080 / Web / the Account :

source code

Guess you like

Origin www.cnblogs.com/7788IT/p/11626855.html