1. 程式人生 > >SpringBoot學習篇二----Thymeleaf的引入

SpringBoot學習篇二----Thymeleaf的引入

####首先,引入maven依賴

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

####修改application.yml,可能原來是application.properties

spring:
 thymeleaf:
   cache: false
   prefix: classpath:/templates/
   suffix: .html
   encoding: UTF-8
   content-type: text/html
   mode: HTML5

####編寫Controller

@Controller
public class HelloController {
    @RequestMapping(value = "/ysk",method = RequestMethod.GET)
    public String myhello(Model model){
        model.addAttribute("name","Y.S.K");
        return "hello";
    }
}

####編寫view,因為springBoot預設是模板位置是templates/

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>hello</title>
    <meta http-equiv="Content-Type" contents="text/html; charset=UTF-8" />
</head>
<body>
<h1 th:text="'hello,'+${name}+'!'">hello</h1>

</body>
</html>

歡快的測試成功了~~
這裡寫圖片描述