在完成配置以後,舉一個簡單的例子,在快速入門工程的基礎上,舉一個簡單的示例來經過Thymeleaf渲染一個頁面。html
@Controller public class HelloController { @RequestMapping("/") public String index(ModelMap map) { // 加入一個屬性,用來在模板中讀取 map.addAttribute("host", "http://blog.didispace.com"); // return模板文件的名稱,對應src/main/resources/templates/index.html return "index"; } }
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8" /> <title></title> </head> <body> <h1 th:text="${host}">Hello World</h1> </body> </html>
如上頁面,直接打開html頁面展示Hello World,可是啓動程序後,訪問http://localhost:8080/
,則是展現Controller中host的值:http://blog.didispace.com
,作到了不破壞HTML自身內容的數據邏輯分離。java
更多Thymeleaf的頁面語法,還請訪問Thymeleaf的官方文檔查詢使用。spring
Thymeleaf的默認參數配置app
若有須要修改默認配置的時候,只需複製下面要修改的屬性到application.properties
中,並修改爲須要的值,如修改模板文件的擴展名,修改默認的模板路徑等。ui
# Enable template caching. spring.thymeleaf.cache=true # Check that the templates location exists. spring.thymeleaf.check-template-location=true # Content-Type value. spring.thymeleaf.content-type=text/html # Enable MVC Thymeleaf view resolution. spring.thymeleaf.enabled=true # Template encoding. spring.thymeleaf.encoding=UTF-8 # Comma-separated list of view names that should be excluded from resolution. spring.thymeleaf.excluded-view-names= # Template mode to be applied to templates. See also StandardTemplateModeHandlers. spring.thymeleaf.mode=HTML5 # Prefix that gets prepended to view names when building a URL. spring.thymeleaf.prefix=classpath:/templates/ # Suffix that gets appended to view names when building a URL. spring.thymeleaf.suffix=.html spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain. spring.thymeleaf.view-names= # Comma-separated list of view names that can be resolved.
Spring Boot並不建議使用,但若是必定要使用,能夠參考此工程做爲腳手架:JSP支持spa
完整項目的源碼來源 技術支持1791743380code