TemplateInputException: Error resolving template [/tw/order/publish/user_detail], template might

本文讲述将Windows系统开发的代码部署到Linux系统时,测试功能出现thymeleaf模板解析错误。经排查,发现是路径中多了一个“/”,因Linux系统严格校验路径导致问题。修改Java代码中访问HTML的路径去掉“/”后,重新部署问题解决,并总结了不同配置下的路径设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误信息

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

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值