SpringBoot-使用thymeleaf模板遇到的一些問題
阿新 • • 發佈:2019-01-06
使用springboot+thymeleaf遇到一些問題,主要歸為如下幾點:
1.在/templates目錄下建立自定義目錄/my,並在該目錄下建立index.html,程式中如何訪問index.html
解決方式:
在application.properties配置檔案中通過spring.thymeleaf.prefix屬性進行設定,例如設定預設路徑為/templates/my
1.在/templates目錄下建立自定義目錄/my,並在該目錄下建立index.html,程式中如何訪問index.html
2.如果不使用/templates目錄作為預設路徑,該如何配置
問題1
解決方式:
在controller層方法中通過設定ModelAndView名稱的為:my/index,然後返回該ModelAndView,然後該介面方法時就會跳轉到index.html
示例程式碼如下:
問題2@RequestMapping(value="getIndex") public ModelAndView getIndex(ModelAndView model)throws Exception { //訪問自定義目錄下/templates/my/index.html,要注意路徑格式 model.setViewName("my/index"); return model; }
解決方式:
在application.properties配置檔案中通過spring.thymeleaf.prefix屬性進行設定,例如設定預設路徑為/templates/my
示例程式碼如下:
spring.thymeleaf.prefix=classpath:/templates/my
springboot+thymeleaf使用的程式碼如下: