在完成配置之后,舉一個(gè)簡(jiǎn)單的例子,在快速入門(mén)工程的基礎(chǔ)上,舉一個(gè)簡(jiǎn)單的示例來(lái)通過(guò)Thymeleaf渲染一個(gè)頁(yè)面。
- @Controller
- public class HelloController {
- @RequestMapping("/")
- public String index(ModelMap map) {
- // 加入一個(gè)屬性,用來(lái)在模板中讀取
- map.addAttribute("host", "http://blog.didispace.com");
- // return模板文件的名稱,對(duì)應(yīng)src/main/resources/templates/index.html
- return "index";
- }
- }
復(fù)制代碼- <!DOCTYPE html>
- <html>
- <head lang="en">
- <meta charset="UTF-8" />
- <title></title>
- </head>
- <body>
- <h1 th:text="${host}">Hello World</h1>
- </body>
- </html>
復(fù)制代碼如上頁(yè)面,直接打開(kāi)html頁(yè)面展現(xiàn)Hello World,但是啟動(dòng)程序后,訪問(wèn)http://localhost:8080/,則是展示Controller中host的值:http://blog.didispace.com,做到了不破壞HTML自身內(nèi)容的數(shù)據(jù)邏輯分離。 更多Thymeleaf的頁(yè)面語(yǔ)法,還請(qǐng)?jiān)L問(wèn)Thymeleaf的官方文檔查詢使用。 Thymeleaf的默認(rèn)參數(shù)配置 如有需要修改默認(rèn)配置的時(shí)候,只需復(fù)制下面要修改的屬性到application.properties中,并修改成需要的值,如修改模板文件的擴(kuò)展名,修改默認(rèn)的模板路徑等。 - # 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.
復(fù)制代碼 支持JSP的配置Spring Boot并不建議使用,但如果一定要使用,可以參考此工程作為腳手架:JSP支持
|