报错代码如下:
@GetMapping("/chat/lt")
public String tochat(){
return "/chat/ltzy";
}
application.xml配置如下:
thymeleaf:
suffix: .html
prefix:
classpath: /templates/
cache: false
项目在IDEA上运行,可正常跳转到ltzy.html。但是我在cmd用mvn compile package打包后执行jar文件后,跳转失败,报错如标题。
原因在于
return "/chat/ltzy";
去掉第一个“/”,通过类似相对路径的方式来引用,这里的相对路径仍然是相对于模板根目录classpath: /templates/来做的。重新打包执行就可以正常跳转。

本文探讨了在mvn打包后,Spring MVC项目中使用Thymeleaf遇到的跳转路径问题,指出去掉路径前'/'的重要性,并提供了针对模板路径的正确处理方式。

257

被折叠的 条评论
为什么被折叠?



