2021年1月18日 星期一

[SpringBoot 1.5] 使用 thymeleaf

 在pom.xml檔案 這一段加入properites中

<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<!--布局功能的支持程序 thymeleaf3 主程序 layout2以上版本-->
<!--thymeleaf2 layout1 适配-->
<thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version>

然後 dependencies中加入這段

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


因為thymeleaf 在SpringBoot的autoconfigure包中已經有配置好


package org.springframework.boot.autoconfigure.thymeleaf;

import java.nio.charset.Charset;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.MimeType;
@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";


因此以上項目, 如果頁面有放在templates資料夾下面, 他就能使用已經配置好的tymeleaf功能

另外一部分還要在頁面的頭部加入以下內容


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

以下我們示範一個簡單的範例


畫面:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>成功</h1>

<div th:text="${hello}">這是顯示歡迎訊息</div>
</body>
</html>

------------------------------------------------------------------------

java code:

@Controller

public class HelloController {

@RequestMapping("/success")
public String success(Map<String,Object> map){
map.put("hello","您好");
return "success";
}
}


沒有留言:

張貼留言

[leetcode] [KMP] KMP

ABCDABD... ABCDABF... 簡單的說, 傳統解兩字串匹配部分 可能會來個雙迴圈, 哀個比對, 當不匹配的時候, 會將下方列再後移1位 然後不匹配再後移 然而 如果像上放已經有4個屬於匹配的字串, 她就應該直接往後移四位來匹配, 而不是只移動1位 隱藏的思維是, 當...