Spring Boot - 多个模板位置

时间:2016-09-09 14:35:41

标签: spring spring-mvc spring-boot thymeleaf

我希望Spring在不同的位置查找模板,而不仅仅是@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

但是当我尝试访问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) 中的现有模板 index.html 时,我得到了:

def cookies
  # helpers not available in --api mode
  request.cookie_jar
end

如何解决此问题?

2 个答案:

答案 0 :(得分:6)

1。非Spring管理的SpringResourceTemplateResolver

使用当前设置,您将在SpringResourceTemplateResolver方法中创建@Bean NOT 的实例。因此,它不会被春天捡起来,你需要提供ApplicationContextexample):

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

注意resolver.setApplicationContext(applicationContext);

2。 Spring管理SpringResourceTemplateResolver

您可能会看到不需要这样做的示例,但他们肯定会在SpringResourceTemplateResolver方法中创建@Bean实例。 Spring会选择它并为您设置应用程序上下文(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;
}

答案 1 :(得分:3)

如果模板解析器未定义为@Bean,则ThymeLeaf会跳闸。您可以使用此替换WebConfig类。

@Configuration
public class WebConfig{
    @Bean
    public ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setPrefix("static/client");
        // For Spring Boot
        // resolver.setPrefix("classpath:/templates/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("HTML5");
        resolver.setCacheable(false);
        return resolver;
    } 
}

新模板将被选中并添加到模板解析器列表中,有效地执行与“templateEngine.addTemplateResolver()”相同的操作。