我希望在一个项目中有两个模板解析器BEAN。
第一个模板解析器已经在使用。它运作良好。
@Bean
public ITemplateResolver feTemplateResolver(SpringResourceResourceResolver thymeleafResourceResolver) {
String prefix = "classpath:fe20/is/templates/";
boolean cacheable = true;
if (environment.acceptsProfiles(LOCAL)) {
String localResourcePath = environment.getRequiredProperty("FEB_HOME");
prefix = "file:///" + localResourcePath + "/is/templates/";
cacheable = false;
}
TemplateResolver resolver = new TemplateResolver();
resolver.setResourceResolver(thymeleafResourceResolver);
resolver.setPrefix(prefix);
resolver.setSuffix(".html");
resolver.setTemplateMode(StandardTemplateModeHandlers.LEGACYHTML5.getTemplateModeName());
resolver.setCharacterEncoding("UTF-8");
resolver.setCacheable(cacheable);
resolver.setOrder(0);
return resolver;
}
我添加了用于百里香的anthoer旋转变压器,
@Bean
public ServletContextTemplateResolver servletContextTemplateResolver() {
ServletContextTemplateResolver servletContextTemplateResolver = new ServletContextTemplateResolver();
servletContextTemplateResolver.setPrefix("classpath:resources/templates/");
servletContextTemplateResolver.setSuffix(".html");
servletContextTemplateResolver.setTemplateMode("HTML5");
servletContextTemplateResolver.setOrder(1);
return servletContextTemplateResolver;
}
@Bean
public SpringTemplateEngine springTemplateEngine() {
SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
springTemplateEngine.setTemplateResolver(servletContextTemplateResolver());
return springTemplateEngine;
}
@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
thymeleafViewResolver.setTemplateEngine(springTemplateEngine());
return thymeleafViewResolver;
}
然后我运行这个应用程序,请求正确的URL,然后我收到错误
HTTP状态500 - 请求处理失败;嵌套异常是 org.thymeleaf.exceptions.TemplateInputException:解析错误 模板"统计/索引",模板可能不存在或可能不存在 任何配置的模板解析器都可以访问
问题是, 通过这么多麻烦, 我通过删除SpringTemplateEngin / ThymeleafViewResolver BEAN解决了这个问题。所以剩下的BEAN只是ServletContextTemplateResolver。
为什么ThymeleafViewResolver不起作用?
当我只使用ServletContextTemplateResolver BEAN时它为什么会起作用?