解决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
上一篇:详解Java-Jackson使用
栏 目:JAVA代码
下一篇:java文件上传技术深入剖析
本文标题:解决Springboot项目打包后的页面丢失问题(thymeleaf报错)
本文地址:http://www.codeinn.net/misctech/193535.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




