spring boot:thymeleaf模板中insert/include/replace三種引用fragment方式的區別(spring boot 2.3.3)


一,thymeleaf模板中insert/include/replace三種引用fragment方式的區別

insert: 把整個fragment(包括fragment的節點tag)插入到當前節點內部,

replace:用fragment(包括fragment的節點tag)替換掉當前節點

include:把fragment的內容(不包括fragment的節點)插入到當前節點內容

文字描述還是不夠直觀,

看代碼的測試更容易了解三者的區別

 

說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest

         對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/

說明:作者:劉宏締 郵箱: 371125307@qq.com

 

二,演示項目的相關信息

1,項目地址:

https://github.com/liuhongdi/fragment

 

2,功能說明:

        演示了thymeleaf模板:insert/include/replace三種引用fragment方式的區別

 

3,項目結構:如圖:

 

 

三,配置文件說明

1,pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
       <!--thymeleaf begin-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

 

2,application.properties

#error
server.error.include-stacktrace=always
#error
logging.level.org.springframework.web=trace

#thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

 

四,java代碼說明

1,HomeController.java

@RequestMapping("/home")
@Controller
public class HomeController {
    @GetMapping("/home")
    public String index(ModelMap modelMap) {
        return "home/home";
    }
}

 

2,footer.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<span style="background: #ffff00;" th:fragment="copytxt">
    © 2013-2019 數據文化傳播有限公司版權所有
</span>
</body>
</html>

 

3,home.html

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div style="width:100%;height:30px;background:#ffffff;font-size: 16px;" ></div>
<div id="content" style="width:1040px;">
    <div style="width:790px;float:left;margin-left:30px;">
        <!--main begin-->
        <div style="background: #ff0000;width:400px;height:100px;" th:insert="common/footer :: copytxt"></div>
        <div style="background: #00ff00;width:400px;height:100px;" th:replace="common/footer :: copytxt"></div>
        <div style="background: #0000ff;width:400px;height:100px;" th:include="common/footer :: copytxt"></div>
        <!--main   end-->
    </div>
</div>
</body>
</html>

分別用三種方式引用footer中的copytxt片斷,

比較異同

 

五,測試效果

1,訪問home:

http://127.0.0.1:8080/home/home

如圖:

 

查看源代碼比較:

  <!--main begin-->
        <div style="background: #ff0000;width:400px;height:100px;"><span style="background: #ffff00;">
    © 2013-2019 數據文化傳播有限公司版權所有
</span></div>
        <span style="background: #ffff00;">
    © 2013-2019 數據文化傳播有限公司版權所有
</span>
        <div style="background: #0000ff;width:400px;height:100px;">
    © 2013-2019 數據文化傳播有限公司版權所有
</div>
        <!--main   end-->

可以看到三者的區別:

insert:把整個fragment插入到當前節點內部,

replace:用fragment替換到當前節點

include:把fragment的內容(不包括fragment的節點)插入到當前節點

 

六,查看spring boot版本:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.3.RELEASE)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM