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

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

点赞(250)

评论列表共有 0 条评论

立即
投稿
返回
顶部