美文网首页spring boot我爱编程Spring-Boot
在spring boot中使用bootstrap+thymele

在spring boot中使用bootstrap+thymele

作者: Angeladaddy | 来源:发表于2016-12-28 15:19 被阅读6437次

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

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

相关文章

网友评论

  • 程序猿KK:试了下,你的这种方法貌似不行哦,还是一样报错。

    我采用的方式是:spring.thymeleaf.mode=LEGACYHTML5

    这样就OK了,可以改thymeleaf的版本,也可以不改
    Angeladaddy:试了一下,LEGACYHTML5可以,但是thymeleaf版本不改就报错

本文标题:在spring boot中使用bootstrap+thymele

本文链接:https://www.haomeiwen.com/subject/agpyvttx.html