SpringBoot 之 thymeleaf

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 

相關文章
相關標籤/搜索