springboot中thymeleaf 配置多個路徑實現測試過程
阿新 • • 發佈:2022-03-28
測試環境:win10.JDK 1.8
需求背景:springboot 配合 thymeleaf 自己開發一個小站點。
例如其它路由/xxx/yyyy.html 打包後 正常訪問 jar包體內classpath:/templates/ 下模板,並開啟cache 以加快訪問速度;
此時新增一個功能:例如 路由/outtemplate/zzzz.html 去jar包執行路徑下指定目錄提取模板 且不開啟cache 方便釋出後實時更新,且在java中處理一些通用邏輯。(如果無邏輯處理:可以外部建立static供 springboot自動掃描,html作為靜態檔案訪問)
主要測試類:
ClassLoaderTemplateResolver || SpringResourceTemplateResolver
建立任意類 使用 @Configuration 標籤。我圖方便放在了之前寫的一個類內部
ClassLoaderTemplateResolver 測試:
訪問測試路由:
http://localhost:8081/class/test ;測試寫死模板:classGet.html
classGet.html所在目錄:
編寫ClassLoaderTemplateResolver Bean 將classes 下的test目錄設定為該解析器根目錄
結果訪問 成功;
但是這個還不符合需求,我需要用到外部目錄 則使用 SpringResourceTemplateResolver
SpringResourceTemplateResolver 成功掃到模板,並正常訪問
總結:初步理解兩個類的簡單用法。如果有不對希望大家指正