spring boot thymeleaf相关问题

环境:使用spring boot 1.5.8.RELEASE版本,thymeleaf使用默认的版本(如不配置默认为:2.1.5.RELEASE)

场景一:html中标签没有正确结束或者错误,thymeleaf报错

解决一:加入忽略html错误的maven配置

<nekohtml.version>1.9.22</nekohtml.version>

<!-- Thymeleaf配置为LEGACYHTML5不会强行检查html格式规范,需要在application.properties中spring.thymeleaf.mode=LEGACYHTML5 -->
<dependency>
   <groupId>net.sourceforge.nekohtml</groupId>
   <artifactId>nekohtml</artifactId>
   <version>${nekohtml.version}</version>
</dependency>

--------------------------------

application.properties:
## 配置为LEGACYHTML5不会强行检查html格式规范,需要在pom.xml中引入相关依赖
spring.thymeleaf.mode=LEGACYHTML5

上述针对默认的thymeleaf2版本中亲测有效....

-------------------------------------------华丽的分割线---------------------------------------------------

场景二:手动换成thymeleaf3版本,并舒服的使用th:insert等(其实我就是因为这个目的而发现的)

解决二:

1. 在父项目工程中pom.xml的properties标签中加入如下配置(nekohtml.version仍然留着,去掉好像也可以,项目紧就不去深究了~~)

<properties>
        <java.version>1.8</java.version> 

        <!-- Template Begin -->
        <nekohtml.version>1.9.22</nekohtml.version>
        <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
        <thymeleaf-spring4.version>3.0.2.RELEASE</thymeleaf-spring4.version>
        <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
        <!-- Template End -->

</properties>

2. 子项目pom.xml还是一如既往的引入spring-boot-starter-thymeleaf就好

3. 累赘一下,华丽的使用th:insert

    164935_DUSj_1991646.png

      目的:index.html要引入left.html

由于可能存在切换模板的可能性,我的主属性文件配置为:

### Thymeleaf 配置
## 配置为HTML5会强行检查html格式规范
#spring.thymeleaf.mode=HTML5
## 配置为LEGACYHTML5不会强行检查html格式规范,需要在pom.xml中引入相关依赖
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.prefix=classpath:/templates/thymeleaf
spring.thymeleaf.suffix=.html
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false

ojo ojo ojo ojo ojo ojo 我滴癫鬼唷~

在left.html中

<th:block th:fragment="menu"></th:block>

在index.html中使用  

<th:block th:replace="/seller/left :: menu"></th:block>

完成咯~

--------------------------------------------------------------------------------------

远远没有完成.....................

再来带个参数的

在index.html中(多个参数,隔开传过去,也可以直接在 :: menu后加上 (param1=${'123'},param2=${'456'}) ,效果是一样的   )

<th:block th:insert="/seller/left :: menu" th:with="param1=${'123'},param2=${'456'}"></th:block>

在left.html中

<th:block th:fragment="menu">
  <th:block th:text="${param1}"></th:block></th:block>

 

转载于:https://my.oschina.net/u/1991646/blog/1604078

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值