Please enable Javascript to view the contents

Thymeleaf

 ·  ☕ 3 分鐘  ·  🐻 Cloud

Thymeleaf 可以取代JSP

https://www.thymeleaf.org/documentation.html

Can Thymeleaf be used as a complete substitute for JSP and JSTL?
究竟Thymeleaf能否完全取代JSP and JSTL?
Absolutely. Not only it can, but we strongly encourage you to do so.
絕對可以,而且我們強烈鼓勵你使用。

maven的pom.xml添加依賴

1
2
3
4
<dependency>
      		<groupId>org.springframework.boot</groupId>
      		<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  • static:用於存放靜態資源,例如html、JavaScript、css以及圖片等。
  • templates:用來存放模板引擎Thymeleaf(本質依然是.html檔案)

static網站檔案放入src\main\resources\static底下

index.html放入src\main\resources\templates底下

修改code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.example.demo;

import java.util.ArrayList;
import java.util.List;

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

@Controller
public class H {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	
	@GetMapping("/")
	public String h666(Model model) {
        //Model型別即用來儲存資料供我們Thymeleaf頁面使用
		model.addAttribute("name","kkkkkkkkkk");
		List<String> list = new ArrayList<>();
		list.add("greg");
		list.add("54564");
		list.add("hsfiuwe");
		model.addAttribute("list",list);
		return "index";
        }
	
	@GetMapping("/index.html")
	public String h6() {
		return "index";
	}

}
  • Model是一個特殊的類,相當於維護一個Map一樣,而Model中的資料通過controller層的關聯繫結在view層(即Thymeleaf中)可以直接使用。
  • return “hello”:這個index就是在templates目錄下對應模板(本次為Thymeleaf模板)的名稱,即應該對應hello.html這個Thymeleaf檔案(與頁面關聯預設規則為:templates目錄下返回字串.html)。

index.html加入code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<!-- 使用thymeleaf -->
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>thymeleaf test</title>
</head>

<body>
	<!-- 離線狀態顯示name -->
	<p th:text="${name}">name</p>
	<!-- 找不到${what}不顯示 -->
	<p th:text="${what}">what?</p>
	<p>
		<span th:each="item:${list}" th:text="${item}">ggggggggggg</span>
	</p>

</body>
</html>
  • 畫面顯示

kkkkkkkkkk

greg54564hsfiuwe

  • 分別1:

@GetMapping是在Spring4.3時才有的,在意思上@GetMapping 就是@RequestMapping (method = RequestMethod.GET)

  • 分別2:

@GetMapping 可在method層面上用作處理 http 的請求。
@RequestMapping 可在class層面上配合@GetMapping 使用。

  • 分別3:

@GetMapping 與 @RequestMapping 不能互相交換。
@GetMapping的位置只能在class之內,而@RequestMapping在內外也可以

Springboot官方提供的配置(application.properties)內容有以下:

# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.cache=true # Whether to enable template caching.
spring.thymeleaf.check-template=true # Whether to check that the template exists before rendering it.
spring.thymeleaf.check-template-location=true # Whether to check that the templates location exists.
spring.thymeleaf.enabled=true # Whether to enable Thymeleaf view resolution for Web frameworks.
spring.thymeleaf.enable-spring-el-compiler=false # Enable the SpringEL compiler in SpringEL expressions.
spring.thymeleaf.encoding=UTF-8 # Template files encoding.
spring.thymeleaf.excluded-view-names= # Comma-separated list of view names (patterns allowed) that should be excluded from resolution.
spring.thymeleaf.mode=HTML # Template mode to be applied to templates. See also Thymeleaf's TemplateMode enum.
spring.thymeleaf.prefix=classpath:/templates/ # Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.reactive.chunked-mode-view-names= # Comma-separated list of view names (patterns allowed) that should be the only ones executed in CHUNKED mode when a max chunk size is set.
spring.thymeleaf.reactive.full-mode-view-names= # Comma-separated list of view names (patterns allowed) that should be executed in FULL mode even if a max chunk size is set.
spring.thymeleaf.reactive.max-chunk-size=0 # Maximum size of data buffers used for writing to the response, in bytes.
spring.thymeleaf.reactive.media-types= # Media types supported by the view technology.
spring.thymeleaf.servlet.content-type=text/html # Content-Type value written to HTTP responses.
spring.thymeleaf.suffix=.html # Suffix that gets appended to view names when building a URL.
spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain.
spring.thymeleaf.view-names= # Comma-separated list of view names (patterns allowed) that can be resolved.
分享

cloud
作者
Cloud
蔡逼八工程師