Spring Boot-多个模板位置
我希望Spring在不同的位置查找模板,而不仅仅是在src/main/resource/templates/
。
我这里有
@Configurationpublic 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);
}
}
但是,当我尝试访问中的现有模板 index.html 时/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)
如何解决这个问题?
回答:
回答:
使用当前设置,您将在方法中创建的实例SpringResourceTemplateResolver
,而
@Bean
。因此,它不会在spring被捡起,您需要提供ApplicationContext
(示例):
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();resolver.setApplicationContext(applicationContext);
resolver.setPrefix("/WEB-INF/templates/");
resolver.setTemplateMode(TemplateMode.HTML);
注意 resolver.setApplicationContext(applicationContext);
回答:
您可能会看到不需要这样做的示例,但是它们肯定是SpringResourceTemplateResolver
在@Bean
方法中创建实例的。Spring将其拾取并为您设置应用程序上下文(示例):
@Beanpublic 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-多个模板位置 的全部内容, 来源链接: utcz.com/qa/415850.html