spring boot thymeleaf相关问题

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

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

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

<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版本中亲测有效....缓存

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

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

解决二:spring-boot

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

<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就好code

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

    

      目的: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>
相关文章
相关标签/搜索