javahtmlspring-bootthymeleafspring-thymeleaf

Thymeleaf - Replace common parts in a fragment


I have a file named footerScripts.html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<body>
<div th:fragment="common-scripts">
    <script th:src="@{/assets/javascripts/jquery.min.js}"></script>
    <script th:src="@{/assets/vendor/glightbox/js/glightbox.min.js}"></script>
    <script th:src="@{/assets/vendor/bootstrap/js/bootstrap.bundle.min.js}"></script>
    <script th:src="@{/assets/vendor/aos/aos.js}"></script>
    <script th:src="@{/assets/vendor/swiper/swiper-bundle.min.js}"></script>
    <script th:src="@{/assets/javascripts/plugins.js}"></script>
    <script th:src="@{/assets/javascripts/purecounter_vanilla.js}"></script>
    <script th:src="@{/assets/javascripts/validator.min.js}"></script>
    <script th:src="@{/assets/javascripts/contactform.js}"></script>
    <script th:src="@{/assets/javascripts/particles.min.js}"></script>
    <script th:src="@{/assets/javascripts/script.js}"></script>
    <script th:src="@{/assets/javascripts/main.js}"></script>
</div>
</body>
</html>

and another file : index-new.html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:lang="${#locale != null ? #locale.language : 'en'}" lang="en">
<head>
....

<div id="preloader"></div>

<!-- Common JS Scripts -->
<div th:replace="fragments/footerScripts :: common-scripts"></div>
 
<script type="application/ld+json">
    {
        "@context": "https://schema.org",
        "@type": "FAQPage",
        "mainEntity": [{
            "@type": "Question",
            "name": "<th:block th:text=#{faq.question1}></th:block>",
            "acceptedAnswer": {
                "@type": "Answer",
                "text": "<th:block th:text=#{faq.answer1}></th:block>"
            }
        }]
    }
</script>
</body>
</html>

but I have this error:

2025-03-04 16:34:20.468 [http-nio-8080-exec-4] ERROR [] o.a.c.c.C.[.[.[.[dispatcherServlet]@log(175) - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.thymeleaf.exceptions.TemplateInputException: Error resolving template [fragments/footerScripts], template might not exist or might not be accessible by any of the configured Template Resolvers (template: "index-new" - line 341, col 6)] with root cause
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [fragments/footerScripts], template might not exist or might not be accessible by any of the configured Template Resolvers (template: "index-new" - line 341, col 6)
    at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869)
    at org.thymeleaf.engine.TemplateManager.parseStandalone(TemplateManager.java:250)

enter image description here


Solution

  • The error occurs because fragments/footerScripts is not found, but your file is actually located at common/footerScripts.html. In your index-new.html, update the th:replace statement to correctly reference the file location:

    <div th:replace="common/footerScripts::common-scripts"></div>

    This should resolve the issue since the Thymeleaf resolver needs the correct path relative to your template's directory.