解决Springboot项目打包后的页面丢失问题(thymeleaf报错)

网友投稿 1285 2022-11-23

解决Springboot项目打包后的页面丢失问题(thymeleaf报错)

解决Springboot项目打包后的页面丢失问题(thymeleaf报错)

目录Springboot项目打包后的页面丢失遇到的问题目前找到两种Springboot打包ThymeLeaf报错原因解决办法

Springboot项目打包后的页面丢失

遇到的问题目前找到两种

返回视图路径以/开头,例如 /test/hello

在thymeleaf页面中,引入的页面以/开头,例如:

代码书写规范:

@GetMapping("/about-us")

public String sysInfo(){

return "students/about-us";

}

错误写法:(不要在前面加入"/")

return "/studenLDbGfrCZbnts/about-us";

引入公共模板时,也不要加''/'

正确写法:

总结:在代码编写的过程中,要注意规范书写习惯,避免不必要的问题发生。

Springboot打包ThymeLeaf报错

开发环境

Spring Boot 2.0.2

Thymeleaf 3.0.9

现象

Boot 打包启动后报如下错

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/login", template might not exist or might not be accessible by any of the configured Template Resolvers at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:870) ~[thymeleaf-3.0.9.RELEASE.jar!/:3.0.9.RELEASE] at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.9.RELEASE.jar!/:3.0.9.RELEASE] at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) [thymeleaf-3.0.9.RELEASE.jar!/:3.0.9.RELEASE] at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) [thymeleaf-3.0.9.RELEASE.jar!/:3.0.9.RELEASE] atLDbGfrCZbn org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:354) [thymeleaf-spring5-3.0.9.RELEASE.jar!/:3.0.9.RELEASE] ...

判断为模板视图跳转错误

原因

配置文件错误,模板读取路径错误

解决办法

修改ThymeLeaf配置

具体操作

增加红色的部分

thymeleaf:

mode: HTML

cache: false

prefix: classpath:/templates

/**

* 页面路由

* @param pageName 页面名称

* @param model 基础model绑定常用值

* @return

*/

@ApiOperation(value = "请求页面",notes = "获取页面")

@GetMapping(value = "/page/{pageName}")

public String page(@PathVariable @ApiParam("页面名称")String pageName, @ApiIgnore Model model){

initDefaultModel(model);

String page = pageConfig.getPageMap().get(pageName);

if(page == null){

return "/404";

}

return page;

}

开发环境中遗漏可以正常启动跳转,打包后文件结构产生变化需要指定。

参考文档

Spring Boot gives “TemplateInputException: Error resolving template” when running from jar

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot+RabbitMQ+Redis实现商品秒杀
下一篇:springboot+thymeleaf打包成jar后找不到静态资源的坑及解决
相关文章

 发表评论

暂时没有评论,来抢沙发吧~