Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf: Error Resolving Template

Tags:

java

thymeleaf

I am trying to use layouts/templates with Thymeleaf but I'm getting the following exception.

Exception processing template "user/index": Error resolving template "/layouts/default.html", template might not exist or might not be accessible by any of the configured Template Resolvers

Here is my ThymeleafConfig.java

@Configuration
public class ThymeleafConfig {

    @Bean
    public ServletContextTemplateResolver templateResolver() {
        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("HTML5");
        resolver.setOrder(1);
        return resolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver());
        engine.addDialect(new LayoutDialect());
        engine.addDialect(new SpringSecurityDialect());
        engine.addDialect(new SpringStandardDialect());
        return engine;
    }

    @Bean
    public ThymeleafViewResolver thymeleafViewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        return resolver;
    }
}

I have the following folder structure

webapp/
..WEB-INF/
....views/
......layouts/
........default.html
......user
........index.html

Here is my default.html which is my main layout.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Default</title>
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>

    <header>
        This is a header from default.html
    </header>

    <section layout:fragment="content">
        <p>Content should go here!</p>
    </section>

    <footer>
        Footer from default
        <p layout:fragment="custom-footer">Custom footer here!</p>
    </footer>

    <!-- scripts -->
    <script src="https://code.jquery.com/jquery-2.1.3.min.js" />
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>

</body>
</html>

Here is the index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorator="layouts/default.html">
<head>
    <title>Users</title>
</head>
<body>
<section layout:fragment="content">
    <p>This is a paragraph from content page 1</p>
</section>
<footer>
    <p layout:fragment="custom-footer">This is some footer content from content page 1</p>
</footer>
</body>
</html>

They are in different folders but the pathing should work unless I'm just missing something really silly.

like image 456
jkratz55 Avatar asked Mar 28 '15 15:03

jkratz55


2 Answers

I found my issue. If you specify the suffix in your Thymeleaf config you do not need the .html extension.

@Bean
public ServletContextTemplateResolver templateResolver() {
    ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".html"); // here
    resolver.setTemplateMode("HTML5");
    resolver.setOrder(1);
    return resolver;
}

It should be:

layout:decorator="layouts/default"

Instead of:

layout:decorator="layouts/default.html"

I'm guessing it was effectively looking for layouts/default.html.html which would be a problem.

like image 87
jkratz55 Avatar answered Nov 19 '22 22:11

jkratz55


I solved this issue by having following code:

@Configuration
public class ThymeleafConfig{

@Bean
public SpringTemplateEngine springTemplateEngine()
{
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.addTemplateResolver(htmlTemplateResolver());
    return templateEngine;
}

@Bean
public SpringResourceTemplateResolver htmlTemplateResolver()
{
    SpringResourceTemplateResolver emailTemplateResolver = new SpringResourceTemplateResolver();
    emailTemplateResolver.setPrefix("classpath:/templates/");
    emailTemplateResolver.setSuffix(".html");
    emailTemplateResolver.setTemplateMode(StandardTemplateModeHandlers.HTML5.getTemplateModeName());
    emailTemplateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
    return emailTemplateResolver;
}

}

The trick was to add: emailTemplateResolver.setPrefix("classpath:/templates/");

like image 38
Shehan Simen Avatar answered Nov 19 '22 21:11

Shehan Simen