使用Windows系統開發好代碼,將服務部署到Linux系統,測試功能出現下面的錯誤:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template [/tw/order/publish/user_detail], template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [/tw/order/publish/user_detail], template might not exist or might not be accessible by any of the configured Template Resolvers...
查看以上錯誤信息出現:thymeleaf.exceptions.TemplateInputException,說明是application配置文件的thymeleaf配置問題,
首先,檢查配置文件,配置沒有問題:
spring:
thymeleaf:
prefix: classpath:/templates/
suffix: .html
encoding: UTF-8
查看文件地址是:/tw/order/publish/user_detail ,說明沒有找到這個路徑下的html文件。
然後,本地windows系統又測試了一下,/tw/order/publish/user_detail 路徑下的文件能夠正常訪問,沒有問題。
問題分析:
根據配置文件thymeleaf 路徑的配置,要訪問的地址路徑爲,/templates//tw/order/publish/user_detail,路徑多了一個“/”,按照url 的規則,正確的路徑應該是/templates//tw/order/publish/user_detail,但是Windows系統能夠識別這個路徑,所以問題應該出在 “/” 路徑問題上,Linux系統嚴格校驗路徑,找不到 /templates//tw/order/publish/user_detail 路徑下的html 文件?
修改java代碼中訪問html的路徑,將路徑前面的 “/” 去掉:
ModelAndView mv = new ModelAndView( "tw/order/publish/user_detail");
重新部署到Linux服務,問題解決!
問題總結:
Linux系統嚴格校驗訪問路徑。
若配置文件訪問html路徑爲:prefix: classpath:/templates/,那麼java代碼訪問路徑配置爲, ModelAndView mv = new ModelAndView( "tw/order/publish/user_detail");
若配置文件訪問html路徑爲:prefix: classpath:/templates,那麼java代碼訪問路徑配置加上 “/”, ModelAndView mv = new ModelAndView( "/tw/order/publish/user_detail");