I am creating the spring boot web Application. For templating purpose I am using thymeleaf. I am creating separate html page fragment and I am creating two different layout from these fragments. However, When I connecting the content page I am not getting proper output.
Please check the code snippets/
view resolver
@Configuration @EnableAutoConfiguration @PropertySource("classpath:application.properties") public class WebConfig extends WebMvcConfigurerAdapter { @Bean public TemplateResolver templateResolver() { ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); templateResolver.setPrefix("/WEB-INF/views/"); templateResolver.setSuffix(".html"); templateResolver.setTemplateMode("html5"); return templateResolver; } @Bean public SpringTemplateEngine setTemplateEngine() { SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine(); springTemplateEngine.setTemplateResolver(templateResolver()); return springTemplateEngine; } @Bean public ViewResolver viewResolver(){ ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); viewResolver.setTemplateEngine(setTemplateEngine()); viewResolver.setOrder(1); return viewResolver; } Directory structure
src/ main/ webapp/ WEB-INF/ views/ fragments/ header.html footer.html navBar.html layouts/ appLayout.html baseLayout.html welcome.html appLayout.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> <head> </head> <body> <div th:replace="/fragments/header :: header"></div> <div th:replace="/fragments/navbar :: navbar"></div> <div class="container"> <div layout:fragment="content"> <p>Content goes here...</p> </div> <footer> <div th:replace="/fragments/footer :: footer"></div> </footer> </div> </body> </html> Footer fragment footer.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> </head> <body> <div th:fragment="footer"> Footer </div> </body> </html> Different fragments are created using this method.
main method class aka startup
@ComponentScan(basePackages={"xyz.abc"}) @SpringBootApplication public class AppApplication { public static void main(String[] args) { System.out.println("Started"); SpringApplication.run(AppApplication.class, args); } } welcome.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{layouts/appLayout}"> <head> </head> <body> <th:block layout:fragment="content"> <div class="hero-unit"> <h1>My Content</h1> </div> </th:block> </body> </html> Now when I access the server, I am getting only welcome.html and nothing from appLayout and fragments
What am I missing?
One more issue I have gone through many projects and some of them are keeping views under src/main/resource and I am keeping it under src/main/web/WEB-INF what is the difference between these two approaches?





LayoutDialectintoSpringTemplateEngine?