# 啟用Thymeleaf
## 引入依賴
maven中直接引入
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
```
可以查看依賴關系,發現spring-boot-starter-thymeleaf下面已經包括了spring-boot-starter-web,所以可以把spring-boot-starter-web的依賴去掉.
## 配置視圖解析器
spring-boot很多配置都有默認配置,比如默認頁面映射路徑為
classpath:/templates/*.html
同樣靜態文件路徑為
classpath:/static/
在application.properties中可以配置thymeleaf模板解析器屬性.就像使用springMVC的JSP解析器配置一樣
```
#thymeleaf start
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#開發時關閉緩存,不然沒法看到實時頁面
spring.thymeleaf.cache=false
#thymeleaf end
```
具體可以配置的參數可以查看
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties這個類,上面的配置實際上就是注入到該類中的屬性值.
## 編寫DEMO
### 控制器
```
@Controller
public class HelloController {
private Logger logger = LoggerFactory.getLogger(HelloController.class);
/**
* 測試hello
* @return
*/
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello(Model model) {
model.addAttribute("name", "Dear");
return "hello";
}
}
```