Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Thymeleaf to include html file into another html file

I have been looking at all of the resources available on how to include an html file into another html file using Thymeleaf's th:insert. I am wondering if anyone could tell me what I am doing wrong as I feel like this html looks exactly like the examples I have seen.

My index.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.thymeleaf.org ">
<head>
    <title>Default Page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <div th:insert="templates/Navbar :: navbar">  </div>
</body>
</html>

My Navbar.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.thymeleaf.org ">
<head>
    <meta charset="UTF-8"/>
    <title>NavigationBar</title>
    <link rel="stylesheet" type="text/css" media="all" href="../../css/NavBar.css" th:href="@{/css/NavBar.css}" />
</head>
<body>
    <div>
        <div th:fragment="navbar" class="navbar">
            <div class="navbar-inner">
                <a th:href="@{/}" href="/home"> Home</a>
                <a th:href="@{/}" href="/help">Help</a>
            </div>
        </div>
    </div>
</body>
</html>

Also, here is my project's resource hierarchy via screen shot enter image description here

If I put the code between the into the index.html and link the css file, the navbar shows up and works. So, I am not sure why the insert is not working. Here is my configuration file which has been edited based on examples below:

@Configuration
public class WebPageControllerConfig {

    private SpringTemplateEngine templateEngine;
    private ServletContextTemplateResolver templateResolver;

    @Value("${WebController.startHour}")
    public String startHour;

    @Value("${WebController.endHour}")
    public String endHour;

    @Value("${WebController.numOfSkus}")
    public int numOfSkus;

    @Value("${WebController.skusToQuery}")
    public File skusToQuery;

    @Bean
    public ClassLoaderTemplateResolver webPageTemplateResolver(){
        ClassLoaderTemplateResolver webPageTemplateResolver = new ClassLoaderTemplateResolver();
        webPageTemplateResolver.setPrefix("templates/");
        webPageTemplateResolver.setSuffix(".html");
        webPageTemplateResolver.setTemplateMode("HTML5");
        webPageTemplateResolver.setCharacterEncoding(CharEncoding.UTF_8);
        webPageTemplateResolver.setOrder(1);
        return webPageTemplateResolver;
    }

    /* Old way of trying to configure
    @Bean
    public MessageSource messageSource() {...}

    @Bean
    public ServletContextTemplateResolver setTemplateResolver(){...}

    @Bean
    public SpringTemplateEngine setTemplateEngine() {...}

    @Bean
    public ViewResolver viewResolver() {...}
    End of old configuration       */

    public String getStartHour() {return startHour;}

    public String getendHour() {return endHour;}

    public Object getnumOfSkus() {return numOfSkus;}

    public File getSkusToQuery(){return skusToQuery;}
}
like image 235
Cody Ferguson Avatar asked Sep 20 '17 19:09

Cody Ferguson


2 Answers

Try with:

th:replace="/navbar::navbar"

or

th:insert="/navbar::navbar"

It works for me. No need to specify "template/navbar".

like image 34
MangduYogii Avatar answered Sep 25 '22 15:09

MangduYogii


Change to

th:insert="templates/Navbar :: navbar"

and

th:fragment="navbar"

Configuration example:

import org.apache.commons.lang3.CharEncoding;
import org.springframework.context.annotation.*;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

@Configuration
public class ThymeleafConfiguration {

    @Bean
    @Description("Thymeleaf template resolver serving HTML 5 emails")
    public ClassLoaderTemplateResolver emailTemplateResolver() {
        ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
        emailTemplateResolver.setPrefix("root folder where all thymeleaf files/");
        emailTemplateResolver.setSuffix(".html");
        emailTemplateResolver.setTemplateMode("HTML5");
        emailTemplateResolver.setCharacterEncoding(CharEncoding.UTF_8);
        emailTemplateResolver.setOrder(1);
        return emailTemplateResolver;
    }
}
like image 102
egorlitvinenko Avatar answered Sep 23 '22 15:09

egorlitvinenko