模版引擎
常見的模版引擎有JSP、Velocity、Freemarker和Thymeleaf
Thymeleaf模版
- 使用時需要把html頁面放在classpath:/templates/文件夾下,thymeleaf就能自動渲染
- 創建模版文件,并需要導入thymeleaf的名稱空間
<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 th:text="${title}"></h1> <div th:text="${info}">這里的文本之后將會被覆蓋</div> </body> </html>
- 在controller里面準備模版需要的數據
@Controller public class HelloT {@RequestMapping("/ht")public String ht(Model model) {model.addAttribute("title","hello Thymeleaf").addAttribute("info","this is first thymeleaf test");return "t1";} }
語法規則
th:text --》改變元素里面的文本內容
th:任意的html屬性 --》替換原生的屬性的值;html屬性值只要加上了th:就相當于兼容了Thymeleaf模版
- 參考文檔
- 中文參考
?
?
?