在完成配置以后,举一个简单的例子,在快速入门工程的基础上,举一个简单的示例来经过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