SpringBoot使用Thymeleaf所爬过的坑

2 篇文章 0 订阅
1 篇文章 0 订阅

心血来潮,把Thymeleaf放在了SpringBoot玩了一下,结果很GG。

最开始的时候,是 pom.xml 的 project 标签报错,原因是引入错了maven依赖所导致的,这个不是重点。

基本的环境搭建好之后,启动工程在浏览器运行就报了一个错:org.xml.sax.SAXParseException: The element type "THYMELEAF_ROOT" must be terminated by the matching end-tag "</THYMELEAF_ROOT>".

图片.png SpringBoot使用Thymeleaf所爬过的坑 spring 第1张

意思就是 "THYMELEAF_ROOT" 这个标签没有闭合,在使用springboot的过程中,如果使用thymeleaf作为模板文件,则要求HTML格式必须为严格的html5格式,必须有结束标签,否则会报错。但是然并卵,百度了几分钟之后,找到了一个貌似可以解决的方案:

Java
// 在 application.properties 文件里声明
spring.thymeleaf.content-type=text/html 
spring.thymeleaf.cache=false
spring.thymeleaf.mode =LEGACYHTML5

其他参数:

Java
# 是否使用模板缓存
spring.thymeleaf.cache=true
# 检查模板位置是否存在
spring.thymeleaf.check-template-location=true
# 内容类型
spring.thymeleaf.content-type=text/html
# 是否启用MVC视图解析
spring.thymeleaf.enabled=true
# 编码
spring.thymeleaf.encoding=UTF-8
# 要被排除的视图名
spring.thymeleaf.excluded-view-names=
spring.thymeleaf.view-names=
# 模式
spring.thymeleaf.mode=HTML5
# 前缀
spring.thymeleaf.prefix=classpath:/templates/
# 后缀
spring.thymeleaf.suffix=.html  
spring.thymeleaf.template-resolver-order=


然后启动了,值得高兴的是,没有再报什么标签没有闭合的错误,而是:

图片.png SpringBoot使用Thymeleaf所爬过的坑 spring 第2张

错误消息比较长:

Java
org.thymeleaf.exceptions.ConfigurationException: 
    Cannot perform conversion to XML from legacy HTML: 
    The nekoHTML library is not in classpath. nekoHTML 1.9.15 or newer is required for processing templates in "LEGACYHTML5" mode [http://nekohtml.sourceforge.net]. 
    Maven spec: "net.sourceforge.nekohtml::nekohtml::1.9.15". IMPORTANT: DO NOT use versions of nekoHTML older than 1.9.15.

意思是需要依赖“nekoHTML”,在pom.xml里面添加此依赖即可:

Java
<!-- nokehtml 相关依赖 -->
<dependency>
   <groupId>net.sourceforge.nekohtml</groupId>
   <artifactId>nekohtml</artifactId>
   <version>1.9.21</version>
</dependency>


项目地址:https://github.com/ibubbo/springboot_thymeleaf

遇到一个git问题

更换git用户提交时,报了一个403错误,以前的GitHub账号不用了。

我第一步是更改了用户名和邮箱

Java
git config --global user.name ibubbo
git config --global user.email ibubbo@126.com

接着删除了本地的.ssh,并重新生成了一个添加到了远程git中

 
 
  1. ssh -keygen -t rsa -"ibubbo@126.com"  // 一路回车就行

然后删除了Windows中的git账号信息:

图片.png SpringBoot使用Thymeleaf所爬过的坑 spring 第3张

然而还是没好。。

于是我去请教了一位大神,但是没拿到答案,几分钟过去了....

我再次重新启动git运行推送命令时,奇迹出现了,重新让我输入了用户名和密码,没有在出现403出错。

图片.png SpringBoot使用Thymeleaf所爬过的坑 spring 第4张

关于引入CSS/JS 404问题

在Spring Boot里,默认静态文件存放在static文件夹下,静态模板放在templates下,但是我在写的时候,并没有按照规矩来写,我是这么引入的:

blob.png SpringBoot使用Thymeleaf所爬过的坑 spring 第5张

在引入的时候,不需要加静态文件的文件夹,后来我是重新指定了静态文件的位置,才没有报错了:

blob.png SpringBoot使用Thymeleaf所爬过的坑 spring 第6张

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值