解決在Thymeleaf中layout:fragment不生效的問題

       最近一直在springboot項目上使用thymeleaf模板,很是方便,於是在我的SpringMVC項目中整合了thymeleaf模板,但是就出問題了,layout:fragment並不生效,模板繼承並不可以使用。百般查閱之後,解決方法如下:


這裏寫圖片描述

意思呢就是要想使用這個fragment呀,首先得在bean配置中加上這麼幾句纔可以。

首先是maven依賴:

<dependency>
      <groupId>nz.net.ultraq.thymeleaf</groupId>
      <artifactId>thymeleaf-layout-dialect</artifactId>
      <version>2.2.1</version>
</dependency>

然後是在啓動類增加:

TemplateEngine templateEngine = new TemplateEngine(); 
templateEngine.addDialect(new LayoutDialect());

或者是在XML配置文件中增加:

<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
  <property name="additionalDialects">
    <set>
      <bean class="nz.net.ultraq.thymeleaf.LayoutDialect"/>
    </set>
  </property>
</bean>

參考:Thymeleaf Layout Dialect官方API

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章