如何使用Thymeleaf整合一个共享布局,并在项目中的所有html页面中使用该布局?

问题描述 投票:0回答:1

我目前所做的是这样的,但很明显是行不通的,我想在所有的页面中使用这个页眉和页脚,如何使共享布局并在其他项目中使用?我想在所有的页面中使用这个页眉和页脚,如何制作一个共享布局并在其他页面中使用它?我已经给出了图片以及代码。请帮我提供一个使用main_layout.html的html页面的示例代码。

由于我对Thymeleaf很陌生,所以在代码中可能会有一些愚蠢的错误,请你帮我提供一个使用main_layout.html的html页面的示例代码。

Than you in advance.

But obviously it's not working. I want to use this header and footer in all the pages how to make a shared layout and use it in  other pages??

HEADER.HTML

<header class="site-header" th:fragment="header">
    <div class="container">
        <a href="index.html" id="branding">
            <img src="/dummy/logo.png" alt="Site Title">
            <small class="site-description">Slogan goes
                here</small>
        </a> <!-- #branding -->
        <nav class="main-navigation">
            <button type="button" class="toggle-menu"><i class="fa 
          fa-bars"></i></button>
            <ul class="menu">
                <li class="menu-item"><a href="../views/index.html">Home</a></li>
                <li class="menu-item"><a href="../views/about.html">About</a></li>
                <li class="menu-item"><a href="../views/gallery.html">Gallery</a></li>
                <li class="menu-item"><a href="../views/download.html">Download</a></li>
                <li class="menu-item"><a href="../views/blog.html">Blog</a></li>
                <li class="menu-item"><a href="../views/contact.html">Contact</a></li>
            </ul> <!-- .menu -->
        </nav> <!-- .main-navigation -->
        <div class="mobile-menu"></div>
    </div>
</header> <!-- .site-header -->

FOOTER.HTML

<footer class="site-footer" th:fragment="footer">
    <div class="container">
        <img src="dummy/logo-footer.png" alt="Site Name">

        <address>
            <p>495 Brainard St. Detroit, MI 48201 <br><a href="tel:354543543">(563) 429 234 890</a> <br> <a
                    href="mailto:[email protected]">[email protected]</a></p>
        </address>

        <form action="#" class="newsletter-form">
            <input type="email" placeholder="Enter your email to 
join newsletter...">
            <input type="submit" class="button cut-corner" value="Subscribe">
        </form> <!-- .newsletter-form -->

        <div class="social-links">
            <a href="#"><i class="fa fa-facebook-square"></i></a>
            <a href="#"><i class="fa fa-twitter"></i></a>
            <a href="#"><i class="fa fa-google-plus"></i></a>
            <a href="#"><i class="fa fa-pinterest"></i></a>
        </div> <!-- .social-links -->

        <p class="copy">Copyright 2014 Company Name. Designed by
            Themezy. All right reserved</p>
    </div>
</footer> <!-- .site-footer -->

MAIN_LAYOUT.HTML

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">

<head>

    <!-- Loading third party fonts -->
    <link href="http://fonts.googleapis.com/css? 
family=Source+Sans+Pro:300,400,600,700,900" rel="stylesheet" type="text/css">
    <link href="fonts/font-awesome.min.css" th:href="@{fonts/font- 
awesome.min.css}" rel="stylesheet" type="text/css">
    <!-- Loading main css file -->
    <link rel="stylesheet" href="style.css">

    <!--[if lt IE 9]>
<script src="js/ie-support/html5.js"></script>
<script src="js/ie-support/respond.js"></script>../../static/
<![endif]-->

</head>

<body class="header-collapse">

    <div id="site-content">
        <div th:replace="fragments/header::header"></div>
        <div th:replace="fragments/footer::footer"></div>
    </div> <!-- #site-content -->

    <script th:src="@{js/jquery-1.11.1.min.js}"></script>
    <script th:src="@{js/plugins.js}"></script>
    <script th:src="@{js/app.js}"></script>

</body>

</html>

INDEX.HTML

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
    layout:decorate="~{fragments/main_layout}">

<head>

</head>

<body>

    <div class="hero">
        <div class="slider">
            <ul class="slides">
                <li class="lazy-bg" data-background="dummy/slide-1.jpg">
                    <div class="container">
                        <h2 class="slide-title">Header goes here</h2>
                        <h3 class="slide-subtitle">Less important text goes here</h3>
                        <p class="slide-desc">Lorem ipsum dolor sit amet, consectetur adipisicing elit. <br>Fugiat
                            aliquid minus nemo sed est.</p>

                        <a href="#" class="button cut-corner">Read More</a>
                    </div>
                </li>
                <li class="lazy-bg" data-background="dummy/slide-2.jpg">
                    <div class="container">
                        <h2 class="slide-title">Header goes here</h2>
                        <h3 class="slide-subtitle">Less important text goes here</h3>
                        <p class="slide-desc">Lorem ipsum dolor sit amet, consectetur adipisicing elit. <br>Fugiat
                            aliquid minus nemo sed est.</p>

                        <a href="#" class="button cut-corner">Read More</a>
                    </div>
                </li>
                <li class="lazy-bg" data-background="dummy/slide-3.jpg">
                    <div class="container">.........

enter image description here这里是页眉,页脚和布局,如果有人想编辑和发布.索引也在这里,我使用的布局.但输出是像我在第2个图像中显示。无论我在索引页内写的是什么内容,都不会在输出中显示出来。

where i'm using the layout. but the output comes like as I'm showing in the 2nd image. And whatever I'm writing inside the index page as the content, nothing shows in the output

java spring-boot spring-mvc thymeleaf template-engine
1个回答
1
投票

你漏掉了指定页面内容的位置,在页面内 MAIN_LAYOUT.HTML:

<div id="site-content">
        <div th:replace="fragments/header::header"></div>
        <!-- ** div for content fragment - this was missed ** -->
        <div layout:fragment="content"> Page content </div>

        <div th:replace="fragments/footer::footer"></div>
    </div> <!-- #site-content -->

还有: layout 上述声明应包括在 INDEX.HTML 这样百里叶就可以解析并将您的内容放置在这个 div.

<body> 
<!-- ** layout container - this was missed **-->
    <div layout:fragment="content">
    <!-- your page content -->
    </div
</body>
© www.soinside.com 2019 - 2024. All rights reserved.