I am trying to use layouts / templates with Thymeleaf, but I am getting the following exception.
User / index exception handling template: error resolution template "/layouts/default.html", the template may not exist or may not be accessible by any of the configured Resolvers templates
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 location.
<!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> <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 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 path should work if I just donโt miss something really stupid.
source share