在視圖設(shè)計中镀钓,一般情況下都會采用Thymeleaf模板进栽,那Thymeleaf模板有什么功能呢雷滋?
Thymeleaf是Web和獨立環(huán)境的現(xiàn)代服務(wù)器端Java模板引擎类缤,能夠處理HTML臼勉,XML,JavaScript呀非,CSS甚至純文本坚俗。它具有豐富的標(biāo)簽語言和函數(shù)镜盯,主要目標(biāo)是提供一種優(yōu)雅和高度可維護的創(chuàng)建模板的方式岸裙。
Thymeleaf 是新一代的模板引擎,在spring4.0中推薦使用thymeleaf來做前端模版引擎速缆,因為Springboot默認(rèn)是不支持JSP的降允,所以使用Spring Boot 框架進行界面設(shè)計,一般都會用Thymeleaf 模板艺糜。
鏈接:https://www.cnblogs.com/ityouknow/p/5833560.html
http://www.cnblogs.com/chenlove/p/9375756.html
二剧董、Spring Boot使用Thymeleaf
1幢尚、引入Thymeleaf依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、配置Thymeleaf
#thymelea模板配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
PS:說明一下翅楼,這些配置不是必須的尉剩,如果配置了會覆蓋默認(rèn)的。
在開發(fā)時建議將spring.thymeleaf.cache設(shè)置為false毅臊,否則會有緩存理茎,導(dǎo)致頁面沒法及時看到更新后的效果。比如你修改了一個文件管嬉,已經(jīng)update到tomcat皂林,但刷新頁面還是之前的頁面,就是因為緩存引起的蚯撩。
3础倍、編寫模板文件src/main/resouces/templates/helloHtml.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1 th:inline="text">Hello.v.2</h1>
<p th:text="${hello}"></p>
</body>
</html>
4、編寫訪問路徑(controller)
package com.guxf.demo.controller;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TemplateController {
/**
* 返回html模板.
*/
@RequestMapping("/helloHtml")
public String helloHtml(Map<String,Object> map){
map.put("hello","from TemplateController.helloHtml----就問你簡不簡單胎挎?");
return"/helloHtml";
}
}
5沟启、啟動Application,默認(rèn)輸入:http://127.0.0.1:8080/helloHtml
三犹菇、補充——Spring Boot使用freemarker
使用freemarker也很簡單美浦,在pom.xml加入freemarker的依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
剩下的編碼部分都是一樣的,說下application.properties文件:
########################################################
###FREEMARKER (FreeMarkerAutoConfiguration)
########################################################
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
#spring.freemarker.suffix=.ftl
#spring.freemarker.template-loader-path=classpath:/templates/#comma-separatedlist
#spring.freemarker.view-names= #whitelistofviewnamesthatcanberesolved
剩余的代碼项栏,基本都一樣浦辨,thymeleaf和freemarker是可以共存的。
本文記錄一下幾點:
一沼沈、資源文件的約定目錄結(jié)構(gòu)
二流酬、Maven配置
三、開發(fā)時修改thymeleaf模板自動重新加載配置
四列另、thymeleaf常用基礎(chǔ)知識點
一芽腾、資源文件的約定目錄結(jié)構(gòu)
Maven的資源文件目錄:/src/java/resources
spring-boot項目靜態(tài)文件目錄:/src/java/resources/static
spring-boot項目模板文件目錄:/src/java/resources/templates
spring-boot靜態(tài)首頁的支持,即index.html放在以下目錄結(jié)構(gòu)會直接映射到應(yīng)用的根目錄下:
classpath:/META-INF/resources/index.html
classpath:/resources/index.html
classpath:/static/index.html
calsspath:/public/index.html
如果將index.html模板文件直接放到了/src/java/resources/templates目錄下页衙,那么需配置下才可以訪問到首頁文件摊滔,因為目錄并不是首頁文件的默認(rèn)目錄:
@RequestMapping("/")
public String index(){
return "index";
}
在spring-boot下,默認(rèn)約定了Controller試圖跳轉(zhuǎn)中thymeleaf模板文件的前綴prefix是”classpath:/templates/”,后綴suffix是”.html”
這個在application.properties配置文件中是可以修改的:
spring.thymeleaf.prefix: /templates/
spring.thymeleaf.suffix: .html