Spring Boot 集成 Thymeleaf 布局遇坑

 SpringBoot采用嵌入式的Servlet容器,默认模板解析不支持JSP,需要额外配置,基本上都会初期都会选择SpringBoot推荐的Thymeleaf模板引擎来解析视图;

在刚用Thymeleaf时,但是觉得还行,跟JSP其实也差不了多少,但是自己实际操作起来的时候,还是遇到了坑,被坑的很久的是Thymeleaf的页面布局路径是根据templates下的路径,而不是相对路径;

遇坑过程:

在templates下,我的项目路径如下所示: 

 --templates

  --crud(d)

    --emp(d)

    --commons(d)

      --module.html(f)

    --list.html(f)

    --add.html(f)

module.html里面是我的模板文件:

 然后在emp文件下的list.html进行引入commons文件夹下module.html的公共片段(TMD还有提示,就认为是正确的,高兴不已):

 结果项目一启动访问,后台就一直报500,错误如下:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [crud/emp/list], template might not exist or might not be accessible by any of the configured Template Resolvers
    at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]

 上网查了好几种出现这种情况的解决方式:

  1. 什么使用了@RestController,@ResponseBody(不会范这种错)

  2. Thymeleaf要求HTML5规范,不能出现单标签(一顿解决单标签操作,还是不行,结果thymeleaf3.0早就不要求严格的html5了)

  3. 要引入thymeleaf-layout-dialect依赖(加了还是没有用)

  4. 有人建议清除maven仓库(没办法,把maven本地库删了,重新拉取依赖包,结果还是不行)

十分奔溃,好想有一个厉害的高手,可惜木有。。。

最后在阴差阳错的测试中,发现原来布局的分段路径要以tempaltes路径来引入是okay的,于是把commons整个文件夹放到了templates的目录下(可是在list.hmlt引入布局分段没有之前编写时有路径提示

 本来不报希望的,都没有提示,结果可以访问,欲哭无泪呀,不过总算是解决了。。。

猜你喜欢

转载自www.cnblogs.com/gangbalei/p/11499143.html