Reputation: 7890
I'm trying to configure a second template resolver to be used by Thymeleaf. I also want the default resolver which looks under the templates
folder but whatever I try I only end up with a single resolver.
In my project there is already a yaml configuration file which contains:
thymeleaf:
mode: LEGACYHTML5
cache: false
As a first step I've tried to add a configuration bean:
@Configuration
@EnableWebMvc
public class ThymeleafConfiguration extends WebMvcConfigurerAdapter implements ApplicationContextAware
{
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException
{
this.applicationContext = applicationContext;
}
@Bean
public TemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver());
return engine;
}
private ITemplateResolver templateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("/public/");
resolver.setTemplateMode("HTML");
return resolver;
}
}
But I never see a second resolver in org.thymeleaf.TemplateRepository
, just the default.
I've further tried omdifying the YAML file with the following:
thymeleaf:
-
mode: LEGACYHTML5
cache: false
prefix: classpath:/public/
-
mode: LEGACYHTML5
cache: false
prefix: classpath:/templates/
But again I only get a single resolver created.
Anyone know how to do this or can see what I am doing wrong?
Upvotes: 3
Views: 5126
Reputation: 7171
I find that WebMvcConfigurerAdapter
in org.springframework.web.servlet
cannot download from maven, then find this solution work:
@Bean
public ClassLoaderTemplateResolver secondaryTemplateResolver() {
ClassLoaderTemplateResolver secondaryTemplateResolver = new ClassLoaderTemplateResolver();
secondaryTemplateResolver.setPrefix("templates-2/");
secondaryTemplateResolver.setSuffix(".html");
secondaryTemplateResolver.setTemplateMode(TemplateMode.HTML);
secondaryTemplateResolver.setCharacterEncoding("UTF-8");
secondaryTemplateResolver.setOrder(1);
secondaryTemplateResolver.setCheckExistence(true);
return secondaryTemplateResolver;
}
Upvotes: 0
Reputation: 879
If you want to add multiple resolvers, you could use engine.addTemplateResolver
instead of engine.setTemplateResolver()
or use setTemplateResolvers()
which takes in a Set
.
@Configuration
@EnableWebMvc
public class ThymeleafConfiguration extends WebMvcConfigurerAdapter implements ApplicationContextAware
{
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException
{
this.applicationContext = applicationContext;
}
@Bean
public TemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.addTemplateResolver(templateResolverPublic());
engine.addTemplateResolver(templateResolverTemplates());
return engine;
}
private ITemplateResolver templateResolverPublic() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("/public/");
resolver.setTemplateMode("HTML");
return resolver;
}
private ITemplateResolver templateResolverTemplates() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("/templates/");
resolver.setTemplateMode("HTML");
return resolver;
}
}
P.S. On an other note if you want to use auto-configuration capabilities of Spring boot, you should avoid setting up your configuration manually (as you did in your configuration class) and instead just define properties in YAML which Spring Boot will use and will configure your template engine/resolvers. If you manually specify your configuration spring boot will not additionally configure the Thymeleaf engine/resolvers so the YAML properties w.r.t the configurations you defined will not be used i.e. you are basically overriding spring boots configurations.
Upvotes: 6