解决thymeleaf layout布局不生效


今天使用thymeleaf layout布局时总是不生效,特此把解决问题的步骤和几个关键点记录下来备忘。

一、检查依赖
1.thymeleaf必备maven依赖:
1
2
3
4
5
6
7
8
9
10
<dependency>
     <groupId>org.thymeleaf</groupId>
     <artifactId>thymeleaf</artifactId>
     <version>${thymeleaf.version}</version>
</dependency>
<dependency>
     <groupId>org.thymeleaf</groupId>
     <artifactId>thymeleaf-spring4</artifactId>
     <version>${thymeleaf.version}</version>
</dependency>

   

2.如果使用layout布局,还需要添加:
1
2
3
4
5
<dependency>
     <groupId>nz.net.ultraq.thymeleaf</groupId>
     <artifactId>thymeleaf-layout-dialect</artifactId>
     <version> 2.2 . 2 </version>
</dependency>

  

二、配置视图引擎
1.配置thymeleaf作为视图引擎
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!-- Thymeleaf View Resolver - implementation of Spring's ViewResolver  interface  -->
<bean id= "viewResolver"  class = "org.thymeleaf.spring4.view.ThymeleafViewResolver" >
     <property name= "templateEngine"  ref= "templateEngine"  />
     <property name= "characterEncoding"  value= "UTF-8"  />
</bean>
  
<!-- Thymeleaf Template Engine (Spring4-specific version) -->
<bean id= "templateEngine"  class = "org.thymeleaf.spring4.SpringTemplateEngine" >
     <property name= "templateResolvers" >
         <set>
             <ref bean= "templateResolver"  />
         </set>
     </property>
</bean>
  
<!-- Thymeleaf Template Resolver -->
<bean id= "templateResolver"  class = "org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver" >
     <property name= "prefix"  value= "/WEB-INF/templates/"  />
     <property name= "templateMode"  value= "HTML"  />
     <property name= "suffix"  value= ".html" ></property>
     <property name= "characterEncoding"  value= "UTF-8" ></property>
</bean>

  

2.使用layout还需要在templateEngine添加如下节点:
1
2
3
4
5
6
7
8
<bean id= "templateEngine"  class = "org.thymeleaf.spring4.SpringTemplateEngine" >
     ……
     <property name= "additionalDialects" >
         <set>
             <bean  class = "nz.net.ultraq.thymeleaf.LayoutDialect" />
         </set>
     </property>
</bean>

   

三、页面

task/layout.html

复制代码
<!DOCTYPE html>
<html lang="en" xmlns:layout="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>mysite</title>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="span3" th:insert="fragments/menu::menu"></div>
        <div class="span9" layout:fragment="content"></div>
    </div>
</div>
</body>
</html>
复制代码

index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<html lang= "en"  layout:decorator= "task/layout" >
<head>
     <meta charset= "utf-8" >
     <title>index</title>
</head>
<body>
<div layout:fragment= "content" >
         <h1>
             Welcome!
         </h1>   
</div>
</body>
</html>
 
三、版本号
如果检查了以上几项还是没问题,最后还有一点值得注意的,就是thymeleaf和thymeleaf-layout-dialect的版本。
我最后就是调整了maven依赖的版本号,终于成功了。
 
 

 

    本文转自 陈敬(Cathy) 博客园博客,原文链接:http://www.cnblogs.com/janes/p/7524941.html,如需转载请自行联系原作者



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值