springboot bootstrap和thymeleaf的使用

最近想用springboot自己用业余时间写一个网站,发现坑很多,遇到很多问题,包括前端和后端,前端的基础太不扎实了,还有很多内容需要进一步学习。

这里转一篇bootstrap和thymeleaf在springboot的使用

Spring Boot项目的默认模板引擎是Thymeleaf,这没什么好说的,个人觉得也非常好,因为这款引擎可以前后端同时开发,类似th:xxx这样的内联标签属性会被html5无情忽视,所以前台在开发静态页面的时候就正常开发,正常预览,后台拿来开发好的模板加上这个标签分分钟就开始用,这也是这个引擎的最大好处(你把后台jsp页面在浏览器直接打开就知道这是什么意思了)。
bootstrap作为老牌前台框架,有大量开发好的优秀模板(尤其是各种admin模板),拿来就能用。但是最近在试图把网上下载的一套开源bootstrap模板整合到thymeleaf时遇到一些小问题,经过查阅资料最后解决了,记录一下。

这个问题就是,当你把前台模板直接copy到工程中(css/js/img等直接放在resources/static文件夹下,页面放在resources/templates下),类似这样:

 
Paste_Image.png

login.html中加一句话变成thymeleaf模板

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
...

这时你直接访问这个login.html会报错。
查阅官方文档得知原因:springboot默认使用 Thymeleaf 2.1版本,这个版本无法识别html5中常见的自闭合标签,如<input type="text" />。好弱的感觉。
解决办法时强制更换到Thymeleaf 3:在pom.xml中添加属性:

<properties>
    <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>

然后在application.properties中添加以下一句:
spring.thymeleaf.mode: HTML
注意这一句是必须要加的,需要显式指定。
以下是官方原话:

By default, spring-boot-starter-thymeleaf
uses Thymeleaf 2.1. If you are using the spring-boot-starter-parent
, you can use Thymeleaf 3 by overriding thethymeleaf.version
and thymeleaf-layout-dialect.version
properties, for example:

<properties> 
    <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version> 
    <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>

To avoid a warning message about the HTML 5 template mode being deprecated and the HTML template mode being used instead, you may also want to explicitly configure spring.thymeleaf.mode to be HTML, for example:
spring.thymeleaf.mode: HTML

然后就可以欢快的开撸代码了。



作者:Angeladaddy
链接:https://www.jianshu.com/p/a80d2ff15b47
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
原文地址:https://www.cnblogs.com/linwenbin/p/11170276.html