Spring Boot 如何使用两个Spring资源模板解析器Bean?

g6ll5ycj  于 2022-11-05  发布在  Spring
关注(0)|答案(2)|浏览(170)

从Thymeleaf 3开始,Thymeleaf更喜欢使用SpringResourceTemplateResolverhttps://www.thymeleaf.org/doc/articles/thymeleaf3migration.html),所以我决定从ClassLoaderTemplateResolver升级到SpringResourceTemplateResolver

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.setOrder(1);
    }

    @Bean
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setPrefix("/templates/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode(TemplateMode.HTML);
        resolver.setCharacterEncoding("UTF-8");
        resolver.setOrder(0);
        resolver.setCheckExistence(true);
        return resolver;
    }

    @Bean
    public SpringResourceTemplateResolver templateResolver2() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setPrefix("/templates-2/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode(TemplateMode.HTML);
        resolver.setCharacterEncoding("UTF-8");
        resolver.setOrder(1);
        resolver.setCheckExistence(true);
        return resolver;
    }
}

不幸的是,当这样实现时,我会得到一个错误:Error resolving template [index], template might not exist or might not be accessible by any of the configured Template Resolvers .
老实说,我只是简单地用SpringResourceTemplateResolver替换了ClassLoaderTemplateResolver,希望这样能起作用。它没有。但是在搜索处理两个模板位置的有效解决方案时,我找到的都是使用ClassLoaderTemplateResolvers的过时示例。
尝试实现Thymeleaf提供的代码片段(如此处所示https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html)在使用两个模板目录时也不起作用,此外,此代码本身使用了已弃用的WebMvcConfigurerAdapter。
是否有任何示例说明如何使用Thymeleaf配置Sping Boot 应用程序,该应用程序具有两个或更多尚未完全过时的模板位置?

a8jjtwal

a8jjtwal1#

所以经过一段时间和多次试用,我终于得到了模板的工作。由于没有任何体面的答案,我的问题飞在互联网上,我将张贴我的解决方案给别人:

@Configuration
public class ThymeleafConfig implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(@Autowired ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Bean
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setApplicationContext(this.applicationContext);
        templateResolver.setPrefix("classpath:/templates-1/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setOrder(1);
        templateResolver.setCheckExistence(true); /* FYI: necessary to chain TemplateResolvers */
        templateResolver.setCacheable(false);     /* FYI: during development -> false, so that we can see changes we make */
        return templateResolver;
    }

    @Bean
    public SpringResourceTemplateResolver templateResolver2() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setApplicationContext(this.applicationContext);
        templateResolver.setPrefix("classpath:/templates-2/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setOrder(2);
        templateResolver.setCheckExistence(true); /* FYI: necessary to chain TemplateResolvers */
        templateResolver.setCacheable(false);     /* FYI: during development -> false, so that we can see changes we make */
        return templateResolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        /* SpringTemplateEngine automatically applies SpringStandardDialect and
           enables Spring's own MessageSource message resolution mechanisms. */
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();

        templateEngine.addTemplateResolver(this.templateResolver());
        templateEngine.addTemplateResolver(this.templateResolver2());

        templateEngine.setEnableSpringELCompiler(true);
        return templateEngine;
    }

    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(this.templateEngine());
        viewResolver.setCharacterEncoding("UTF-8");
        viewResolver.setCache(false);   /* FYI: during development -> false */
        viewResolver.setOrder(1);
        return viewResolver;
    }
}

我希望这将帮助其他人保存时间和神经。

3b6akqbq

3b6akqbq2#

如果您使用的是Sping Boot ,则可以在默认解析器之上添加一个额外的解析器,如下所示:
首先,将以下内容添加到application.properties


# This ensures that the default HTML template resolver of Thymeleaf has priority over our custom SVG resolver

spring.thymeleaf.template-resolver-order=0

然后在@Configuration类中添加额外的解析器:

@Configuration
public class MyApplicationConfiguration {

    @Bean
    public ITemplateResolver svgTemplateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setPrefix("classpath:/templates/svg/");
        resolver.setSuffix(".svg");
        resolver.setTemplateMode("XML");

        return resolver;
    }
}

不需要手动定义SpringTemplateEngineViewResolver Bean。
您可以在开发期间使用spring.thymeleaf.cache=false禁用缓存。

相关问题