Spring Boot 集成 Thymeleaf 布局遇坑


 SpringBoot采用嵌入式的Servlet容器,默認模板解析不支持JSP,需要額外配置,基本上都會初期都會選擇SpringBoot推薦的Thymeleaf模板引擎來解析視圖;

在剛用Thymeleaf時,但是覺得還行,跟JSP其實也差不了多少,但是自己實際操作起來的時候,還是遇到了坑,被坑的很久的是Thymeleaf的頁面布局路徑是根據templates下的路徑,而不是相對路徑;

 

遇坑過程:

在templates下,我的項目路徑如下所示: 

 --templates

  --crud(d)

    --emp(d)

    --commons(d)

      --module.html(f)

    --list.html(f)

    --add.html(f)

 

module.html里面是我的模板文件:

 

 然后在emp文件下的list.html進行引入commons文件夾下module.html的公共片段(TMD還有提示,就認為是正確的,高興不已):

 

 結果項目一啟動訪問,后台就一直報500,錯誤如下:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [crud/emp/list], template might not exist or might not be accessible by any of the configured Template Resolvers
    at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]

 

 上網查了好幾種出現這種情況的解決方式:

  1. 什么使用了@RestController,@ResponseBody(不會范這種錯)

  2. Thymeleaf要求HTML5規范,不能出現單標簽(一頓解決單標簽操作,還是不行,結果thymeleaf3.0早就不要求嚴格的html5了)

  3. 要引入thymeleaf-layout-dialect依賴(加了還是沒有用)

  4. 有人建議清除maven倉庫(沒辦法,把maven本地庫刪了,重新拉取依賴包,結果還是不行)

 

十分奔潰,好想有一個厲害的高手,可惜木有。。。

最后在陰差陽錯的測試中,發現原來布局的分段路徑要以tempaltes路徑來引入是okay的,於是把commons整個文件夾放到了templates的目錄下(可是在list.hmlt引入布局分段沒有之前編寫時有路徑提示

 

 本來不報希望的,都沒有提示,結果可以訪問,欲哭無淚呀,不過總算是解決了。。。

 


免責聲明!

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



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