- Spring 5.0 Projects
- Nilang Patel
- 293字
- 2021-07-02 12:34:59
Configuring a Thymeleaf template engine
In order to use the Thymeleaf template engine with Spring MVC, we need to do some configuration wherein we set up the Thymeleaf template engine and update Spring's view resolver to use the template engine to resolve any views. Before moving further, we need to define required dependencies in pom.xml as follows:
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>${thymeleaf.version}</version>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>${thymeleaf-layout-dialect.version}</version>
</dependency>
Let's define the configuration view resolver in order, starting with setting up the template resolver as follows:
@Bean
public ClassLoaderTemplateResolver templateResolver() {
ClassLoaderTemplateResolver templateResolver
= new ClassLoaderTemplateResolver();
templateResolver.setPrefix("templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCacheable(false);
return templateResolver;
}
The previous configuration sets the template location that the template engine will use to resolve the template files. Next is to define the template engine, which will make use of SpringTemplateEngine and the template resolver defined earlier, as follows:
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.addDialect(new LayoutDialect());
return templateEngine;
}
In the previous configuration, we make use of the Thymeleaf Layout Dialect (https://github.com/ultraq/thymeleaf-layout-dialect) created by Emanuel Rabina. This layout dialect helps us in creating a view decorator framework wherein all the templates will be decorated with a base template and the decorated templates just provide the necessary content to complete the page. So all the headers, footers, CSS, scripts, and other common HTML can be placed in the base template. This prevents redundancy to a great extent. In our sample app, the base.html file present in worldgdp/src/main/resources/templates is the base template that is used by other templates.
Next is to define a Thymeleaf view resolver that will override Spring's default view resolver, as follows:
@Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("UTF-8");
return viewResolver;
}
The previous configuration is available in the com.packt.config.ViewConfiguration class.
- OpenLayers Cookbook
- Hands-On Full Stack Development with Spring Boot 2 and React(Second Edition)
- 世界互聯(lián)網(wǎng)發(fā)展報告·2019
- Building RESTful Web services with Go
- 智慧光網(wǎng)絡(luò):關(guān)鍵技術(shù)、應(yīng)用實踐和未來演進
- 邁向自智網(wǎng)絡(luò)時代:IP自動駕駛網(wǎng)絡(luò)
- Echo Quick Start Guide
- 網(wǎng)絡(luò)工程實施技術(shù)與方案大全
- 深入理解計算機網(wǎng)絡(luò)
- 物聯(lián)網(wǎng),So Easy!
- 智能物聯(lián)網(wǎng):區(qū)塊鏈與霧計算融合應(yīng)用詳解
- Python Web Scraping Cookbook
- 網(wǎng)絡(luò)基本通信約束下的系統(tǒng)性能極限分析與設(shè)計
- CiviCRM Cookbook
- 移動應(yīng)用開發(fā)技術(shù)