SpringResourceTemplateResolver

SpringResourceTemplateResolver

本文介绍了Spring Boot - 多个模板位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望 Spring 在不同位置寻找模板,而不仅仅是在 src/main/resource/templates/ 中.

I want Spring look for templates in different locations, not only in src/main/resource/templates/.

这是我所拥有的

@Configuration
public class WebConfig{

    @Autowired
    private SpringTemplateEngine templateEngine;

    @PostConstruct
    public void clientTemplate(){
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setPrefix("static/client");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("HTML5");
        resolver.setOrder(templateEngine.getTemplateResolvers().size());
        resolver.setCacheable(false);
        templateEngine.addTemplateResolver(resolver);
    }
}

但是当我尝试访问 /src/main/resources/static/client 中的现有模板 index.html 时,我得到了:

But when I try to access a existing template index.html in /src/main/resources/static/client, I've got:

java.lang.IllegalArgumentException: ApplicationContext has not been initialized in resource resolver. TemplateResolver or ResourceResolver might not have been correctly configured by the Spring Application Context.
at org.thymeleaf.util.Validate.notNull(Validate.java:37)
at org.thymeleaf.spring4.resourceresolver.SpringResourceResourceResolver.getResourceAsStream(SpringResourceResourceResolver.java:81)
at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:221)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)
at org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335)
at org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:190)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1257)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)

如何解决这个问题?

推荐答案

1.非 Spring 管理的 SpringResourceTemplateResolver

使用您当前的设置,您将在 @Bean 方法中创建 SpringResourceTemplateResolver 的实例,NOT.因此,它不会被 spring 接收,您需要提供 ApplicationContext (示例):

1. Non-Spring managed SpringResourceTemplateResolver

With your current setup, you are creating an instance of SpringResourceTemplateResolver, NOT in a @Bean method. Thus, it doesn't get picked up by spring and you need to provide the ApplicationContext (example):

SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("/WEB-INF/templates/");
resolver.setTemplateMode(TemplateMode.HTML);

注意resolver.setApplicationContext(applicationContext);

您可能会看到不需要这样做的示例,但它们肯定是在 @Bean 方法中创建了 SpringResourceTemplateResolver 实例.Spring 获取它并为您设置应用程序上下文(示例):

You are probably seeing examples which don't need do that, but they are most certainly creating the SpringResourceTemplateResolver instance in a @Bean method. Spring picks it up and sets the application context for you (example):

@Bean
public ITemplateResolver templateResolver() {
    SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
    resolver.setPrefix("/WEB-INF/templates/");
    resolver.setSuffix(".html");
    resolver.setTemplateMode(TemplateMode.HTML);
    resolver.setCacheable(false);
    return resolver;
}

这篇关于Spring Boot - 多个模板位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 04:55