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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

清晨的第一抹阳光   2021-11-11 我要评论
想了解解决Springboot项目打包后的页面丢失问题(thymeleaf报错)的相关内容吗,清晨的第一抹阳光在本文为您仔细讲解Springboot项目打包后的页面丢失的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Springboot项目打包,打包页面丢失,thymeleaf报错,下面大家一起来学习吧。

Springboot项目打包后的页面丢失

遇到的问题目前找到两种

  • 返回视图路径以/开头,例如 /test/hello
  • 在thymeleaf页面中,引入的页面以/开头,例如:<footer th:replace="/index::footer"></footer>

代码书写规范:

    @GetMapping("/about-us")
    public String sysInfo(){
        return "students/about-us";
    }

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

return "/students/about-us";

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

正确写法:

<header th:replace="main/sys-public :: stu-header"></header>

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

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] at 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
<!--公共模板引用 -->
<head th:include="/template/head :: tableHeader"></head>
    /**
     * 页面路由
     * @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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们