In this tutorial, we’ll see how we can define multiple template locations using Thymeleaf in a Spring Boot application.
Firstly, we’ll add the spring-boot-starter-web and spring-boot-starter-thymeleaf Maven dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
By default, Thymeleaf will look for the templates in the templates/ directory on the classpath.
Though we can configure this location with the spring.thymeleaf.prefix property in application.properties:
spring.thymeleaf.prefix=classpath:/templates/
Now, we’ll create a controller to investigate the template path resolution in detail.
@Controller
public class TemplateLocationController {
@RequestMapping("/welcome")
public String sayWelcome() {
return "welcome";
}
}
Here, we have the TemplateLocationController class which has a single endpoint. Since this endpoint returns welcome as the template name and the prefix is classpath:/templates/, the final path becomes classpath:/templates/welcome.html. During the development time, this template resides at src/main/resources/templates/welcome.html – if we use the default Maven folder structure.
@Configuration
public class TemplateResolverConfiguration {
@Bean
public SpringResourceTemplateResolver firstTemplateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setPrefix("classpath:/templates/templatelocation/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setOrder(0);
templateResolver.setCheckExistence(true);
return templateResolver;
}
@Bean
public ClassLoaderTemplateResolver secondTemplateResolver() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("templates/templatelocation/other/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setOrder(1);
templateResolver.setCheckExistence(true);
return templateResolver;
}
@Bean
public ClassLoaderTemplateResolver thirdTemplateResolver() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("templates/templatelocation/another/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setOrder(2);
templateResolver.setCheckExistence(true);
return templateResolver;
}
}
Here, we’re creating one SpringResourceTemplateResolver and two ClassLoaderTemplateResolver beans. During the initialization, we’re assigning different paths using the setPrefix method. Additionally, we’re defining the order of the beans using the setOrder method.
As a result, when a controller method returns a view name, Thymeleaf will look for it in four different locations respectively: /templates/templatelocation/, /templates/templatelocation/other/, /templates/templatelocation/another/ and/templates/.
In this tutorial, we’ve looked at how we can define multiple template locations using Thymeleaf in a Spring Boot application.
Finally, check out the source code for all examples over on Github.
Thymeleaf Multiple Template Locations using Spring Boot
原文:https://www.cnblogs.com/xidianzxm/p/11606743.html