SpringBoot 之 thymeleaf


thymeleaf 的maven 配置我們都知道:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

但是, 它還是很多坑的。

 

先看一個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.thymeleaf.prefix=classpath:/templates/   指明 thymeleaf 查找view 資源的根目錄。 默認是 /templates/ 目錄。 templates 的意思正確理解是模板 文件目錄。

spring.thymeleaf.suffix=.html              指明 thymeleaf  查找view 資源時候使用的 后綴。 默認是 html 文件。

 

上面兩個配置, 我們一般不需要做什么修改。 有意思的是下面的 配置:

spring.thymeleaf.excluded-view-names     要排除的 view Name,也就是 試圖名稱。他可以是一個包含星號(*)的路徑名,(不是正則表達式)。

spring.thymeleaf.view-names=        可以被解析的 view Names, 也可以是一個包含星號的路徑名。只有@RequestMapping返回的view 的name 落在了這個  Comma-separated list , 那么boot 才會使用ThymeleafViewResolver 進行解析。

 

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 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM