thymeleaf 的maven 配置咱们都知道:html
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
可是, 它仍是不少坑的。java
先看一个boot 的默认配置:面试
# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.cache=true # Enable template caching.
spring.thymeleaf.check-template=true # Check that the template exists before rendering it.
spring.thymeleaf.check-template-location=true # Check that the templates location exists.
spring.thymeleaf.content-type=text/html # Content-Type value.
spring.thymeleaf.enabled=true # Enable MVC Thymeleaf view resolution.
spring.thymeleaf.encoding=UTF-8 # Template encoding.
spring.thymeleaf.excluded-view-names= # Comma-separated list of view names that should be excluded from resolution.
spring.thymeleaf.mode=HTML5 # Template mode to be applied to templates. See also StandardTemplateModeHandlers.
spring.thymeleaf.prefix=classpath:/templates/ # Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.suffix=.html # Suffix that gets appended to view names when building a URL.
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.thymeleaf.prefix=classpath:/templates/ , 而 suffix=.html 。这两个配置是至关关键的, 否则就被坑死了!。 它的意思必定不能搞错:spring
spring.thymeleaf.prefix=classpath:/templates/ 指明 thymeleaf 查找view 资源的根目录。 默认是 /templates/ 目录。 templates 的意思正确理解是模板 文件目录。app
spring.thymeleaf.suffix=.html 指明 thymeleaf 查找view 资源时候使用的 后缀。 默认是 html 文件。jsp
上面两个配置, 咱们通常不须要作什么修改。 有意思的是下面的 配置:maven
spring.thymeleaf.excluded-view-names 要排除的 view Name,也就是 试图名称。他能够是一个包含星号(*)的路径名,(不是正则表达式)。spring-boot
spring.thymeleaf.view-names= 能够被解析的 view Names, 也能够是一个包含星号的路径名。只有@RequestMapping返回的view 的name 落在了这个 Comma-separated list , 那么boot 才会使用ThymeleafViewResolver 进行解析。ui
ThymeleafViewResolver 关键代码是:
protected boolean canHandle(String viewName, Locale locale) { String[] viewNamesToBeProcessed = this.getViewNames(); String[] viewNamesNotToBeProcessed = this.getExcludedViewNames(); return (viewNamesToBeProcessed == null || PatternMatchUtils.simpleMatch(viewNamesToBeProcessed, viewName)) && (viewNamesNotToBeProcessed == null || !PatternMatchUtils.simpleMatch(viewNamesNotToBeProcessed, viewName)); }
ThymeleafViewResolver 的处理view 的优先级是第一位,也就是若是它可以处理,那么就交给他处理。 不然交给其余 ViewResolver处理。
咱们能够经过 spring.thymeleaf.template-resolver-order 进行配置。
注意, 若是ThymeleafViewResolver 可以处理, 可是,资源却找不到,那么会看到一个后台错误,如:
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/v222/index", template might not exist or might not be accessible by any of the configured Template Resolvers
at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:246) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
若是ThymeleafViewResolver 不能处理,固然, 是看不到这个错误的。 也就是说, 咱们能够同时使用 ThymeleafViewResolver , 和 JspxxViewResolver, 即同时使用 thymeleaf 模板和 jsp 页面试图技术。 可是他们有个前后顺序。
关于thymeleaf 的语法, 这里不在赘述,请参见:
http://www.cnblogs.com/nuoyiamy/p/5591559.html