Dynamically injecting content in thymeleaf

  1. How to extend the layout.

  2. I want to replace this line

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code><div th:replace="${content}"></div>
    </code>
    <code><div th:replace="${content}"></div> </code>
    <div th:replace="${content}"></div>
    

with the code of main.html.

My admin.html file

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Page</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
<style>
/* Sidebar custom styling */
.sidebar {
background-color: #f8f9fa;
padding: 20px;
height: 100vh;
}
.sidebar a {
color: #000;
text-decoration: none;
display: block;
padding: 10px 0;
}
.sidebar a:hover {
background-color: #e2e6ea;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<!-- Sidebar -->
<div class="col-md-3 col-lg-2 sidebar">
<ul class="list-unstyled">
<li><a href="/">Main</a></li>
<li><a href="home">Home</a></li>
<li><a href="aboutus">About Us</a></li>
</ul>
</div>
<!-- Main Content -->
<div class="col-md-9 col-lg-10">
<div th:fragment="content">
<div th:replace="${content}"></div>
</div>
</div>
</div>
</div>
</body>
</html>
</code>
<code><!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Admin Page</title> <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script> <style> /* Sidebar custom styling */ .sidebar { background-color: #f8f9fa; padding: 20px; height: 100vh; } .sidebar a { color: #000; text-decoration: none; display: block; padding: 10px 0; } .sidebar a:hover { background-color: #e2e6ea; } </style> </head> <body> <div class="container-fluid"> <div class="row"> <!-- Sidebar --> <div class="col-md-3 col-lg-2 sidebar"> <ul class="list-unstyled"> <li><a href="/">Main</a></li> <li><a href="home">Home</a></li> <li><a href="aboutus">About Us</a></li> </ul> </div> <!-- Main Content --> <div class="col-md-9 col-lg-10"> <div th:fragment="content"> <div th:replace="${content}"></div> </div> </div> </div> </div> </body> </html> </code>
<!DOCTYPE html>
<html  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Page</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
    <style>
        /* Sidebar custom styling */
        .sidebar {
            background-color: #f8f9fa;
            padding: 20px;
            height: 100vh;
        }
        .sidebar a {
            color: #000;
            text-decoration: none;
            display: block;
            padding: 10px 0;
        }
        .sidebar a:hover {
            background-color: #e2e6ea;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- Sidebar -->
            <div class="col-md-3 col-lg-2 sidebar">
                <ul class="list-unstyled">
                    <li><a href="/">Main</a></li>
                    <li><a href="home">Home</a></li>
                    <li><a href="aboutus">About Us</a></li>
                </ul>
            </div>

            <!-- Main Content -->
            <div class="col-md-9 col-lg-10">
                <div th:fragment="content">
                    <div th:replace="${content}"></div>
                </div>
            </div>
        </div>
    </div>

</body>
</html>

and main.html file is

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<!-- Extend the layout.html and inject the home-content -->
<div th:replace="layouts/admin :: content">
<div th:fragment="homecontent">
<div class="container">
<h1>Welcome to the Main Page</h1>
<p>This is the Main Page content area. You can place your website's content here.</p>
</div>
</div>
</div>
</body>
</html>
</code>
<code><!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <body> <!-- Extend the layout.html and inject the home-content --> <div th:replace="layouts/admin :: content"> <div th:fragment="homecontent"> <div class="container"> <h1>Welcome to the Main Page</h1> <p>This is the Main Page content area. You can place your website's content here.</p> </div> </div> </div> </body> </html> </code>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <body>

        <!-- Extend the layout.html and inject the home-content -->
        <div th:replace="layouts/admin :: content">
            <div th:fragment="homecontent">
                <div class="container">
                    <h1>Welcome to the Main Page</h1>
                    <p>This is the Main Page content area. You can place your website's content here.</p>
                </div>
            </div>
        </div>

    </body>
</html>

If I go to main route like http://localhost:8080/

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
package com.groceryhub.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("")
public String getMethodName() {
return "admin/main/main";
}
@GetMapping("home")
public String home() {
return "admin/home/home";
}
}
</code>
<code> package com.groceryhub.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping("") public String getMethodName() { return "admin/main/main"; } @GetMapping("home") public String home() { return "admin/home/home"; } } </code>

package com.groceryhub.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;



@Controller
public class HomeController {

    @GetMapping("")
    public String getMethodName() {
        return "admin/main/main";
    }
    

    @GetMapping("home")
    public String home() {
        return "admin/home/home";
    }
    
}

I am getting the below error:

Caused by: org.thymeleaf.exceptions TemplateInputException: Error resolving fragment: “${content}”: template or fragment could not be resolved (template: “layouts/admin” – line 44, col 26)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật