使用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");