8

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;}
}
2
  • 1
    It's th:fragment. Not th:fragments. Also you must use correct paths. Right now you are assuming that your html files are in the same directory but they're not. Commented Sep 20, 2017 at 20:05
  • Thanks! So, I tried the below answer and it didn't work. Then, based off not having the correct path, I changed th:insert="../templates/navbar :: navbar" to use the correct path. This also didn't work. Any further hints? Commented Sep 20, 2017 at 20:31

2 Answers 2

6

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;
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

So, I tried this, but it did not work. Are you also implying to take out class='"navbar"?
Also, with my NavBar.html file having a capital N, should it be templates/Narbar :: navbar ?
I suppose, yes. If this still not work, check your thymeleaf suffix/prefix settings.
Sorry for my ignorance, how to I set the Thymeleaf settings. I see in the tutorial document it talks about the Template Resolver and they use templateResolver.setPrefix and templateResolver.setSuffix, but I do not know where to set these. In my controller class?
There are different approaches. It could be @ Configuration class or properties. For example, stackoverflow.com/questions/29479403/….
|
1

Try with:

th:replace="/navbar::navbar"

or

th:insert="/navbar::navbar"

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.