最近一直在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>