I have my homepage in my spring boot project called home.html. I have a pages controller to render this home.html. Below is the code of the controller.

@RequestMapping(value = "/home")
public String goToHome(Model model) {
    return "home";

In my project home.html is inside webapp/WEB-INF/static/ folder. so the path is webapp/WEB-INF/static/home.html

My home.html looks like below

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/html">
    <object th:include="/en/home :: home-body" th:remove="tag"/>

I have home.html file in an external server as follows.

<div th:fragment="home-body">
    <p>Welcome to home page</p>

The path to this file is http://external-server.com/fragments/en/home.html

What I want to do is to render this home-body fragment part fetching from this external server inside the home.html residing in spring boot tomcat server.

I added the below Bean as a configuration to fetch this home-body fragment. Below is the code for that.

    public UrlTemplateResolver urlTemplateResolver(){
        UrlTemplateResolver urlTemplateResolver = new UrlTemplateResolver();
        boolean isCmsContentEnable = Boolean.parseBoolean( env.getProperty( ConfigurationConstants.CMS_CONTENT_ENABLE ) );
        if( isCmsContentEnable )
            urlTemplateResolver.setPrefix( "http://external-server.com/fragments/" );
            urlTemplateResolver.setSuffix( ".html" );
            urlTemplateResolver.setCharacterEncoding( "UTF-8" );
            urlTemplateResolver.setTemplateMode( "HTML5" );
            urlTemplateResolver.setCacheable( false );
        return urlTemplateResolver;

Now when I try to go to home page, what happens is instead of rendering the home-body fragment inside the home.html residing in the spring boot tomcat server, it directly renders the home.html residing in the external server.

How can I fix this issue? Can anyone point out what I am doing wrong here. Thanks

1 Answers

Supun Dharmarathne On Best Solutions

Create SpringTemplateEngine with thymeleafTemplateResolver and urlTemplateResolver as templateResolvers.

   @Bean public ThymeleafViewResolver thymeleafViewResolver()
    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setTemplateEngine( templateEngine() );
    return viewResolver;

@Bean public SpringTemplateEngine templateEngine()
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    Set<ITemplateResolver> templateResolvers = new HashSet();
    templateResolvers.add( thymeleafTemplateResolver() );
    templateResolvers.add( urlTemplateResolver() );
    templateEngine.setTemplateResolvers( templateResolvers );

    return templateEngine;

@Bean public SpringResourceTemplateResolver thymeleafTemplateResolver()
    SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
    templateResolver.setPrefix( "/WEB-INF/static/" );
    templateResolver.setSuffix( ".html" );
    templateResolver.setTemplateMode( "HTML5" );
    templateResolver.setOrder( 2 );
    return templateResolver;

@Bean public UrlTemplateResolver urlTemplateResolver()
    UrlTemplateResolver urlTemplateResolver = new UrlTemplateResolver();
    urlTemplateResolver.setCharacterEncoding( "UTF-8" );
    urlTemplateResolver.setTemplateMode( "HTML5" );
    urlTemplateResolver.setCacheable( false );
    urlTemplateResolver.setOrder( 3 );
    return urlTemplateResolver;

Then access the external template like this.

 <div th:include="http://banner.server.com/myBanner.html :: fragmentName">