Java 错误org.apache.velocity:ResourceManager:无法找到资源';xxx.html.vm';在任何资源加载器中
我正在使用Velocity模板和Spring boot 当templates目录中有一个名为“xxx.vm”的文件时,Spring Boot成功加载了“xxx.vm”。但下面记录了一条错误消息 “错误org.apache.velocity:ResourceManager:无法在任何资源加载器中找到资源'xxx.html.vm'。 我不明白为什么系统会查找“xxx.html.vm”,因为application.properties中的后缀设置为“.vm” 下面是application.properties中的配置Java 错误org.apache.velocity:ResourceManager:无法找到资源';xxx.html.vm';在任何资源加载器中,java,spring,spring-boot,velocity,resourcemanager,Java,Spring,Spring Boot,Velocity,Resourcemanager,我正在使用Velocity模板和Spring boot 当templates目录中有一个名为“xxx.vm”的文件时,Spring Boot成功加载了“xxx.vm”。但下面记录了一条错误消息 “错误org.apache.velocity:ResourceManager:无法在任何资源加载器中找到资源'xxx.html.vm'。 我不明白为什么系统会查找“xxx.html.vm”,因为application.properties中的后缀设置为“.vm” 下面是application.proper
spring.velocity.enabled=true
spring.velocity.resource-loader-path=classpath:/templates/
spring.velocity.suffix=.vm
运行我的应用程序没有问题,
但是我想知道是什么导致了这个错误消息。
你能帮我处理这件事吗?提前感谢。将以下行添加到
应用程序.属性中
:
spring.velocity.view-names=xxx,yyy,zzz
这是因为spring boot根据类路径中的可用内容配置各种ViewResolver 如果在类路径中找到velocity依赖项,那么spring将配置VelocityViewResolver,但同时它也配置其他视图解析器,ContentNegotingViewResolver就是其中之一 ContentNegotiatingViewResolver尝试匹配视图名称和MIME类型,以自动确定最佳视图。在此过程中,它尝试查找XXX.vm.html,因此引发异常 要修复此问题,请手动配置视图解析器。 参考: 我通过引入以下类手动配置了ViewResolver,问题就消失了
@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
@Autowired
private final ResourceLoader resourceLoader = new DefaultResourceLoader();
@Bean
public VelocityConfig velocityConfig() {
VelocityConfigurer cfg = new VelocityConfigurer();
cfg.setResourceLoader(resourceLoader);
cfg.setResourceLoaderPath("classpath:/templates/")
return cfg;
}
@Bean
public ViewResolver viewResolver() {
VelocityViewResolver resolver = new VelocityViewResolver();
resolver.setViewClass(VelocityToolboxView.class);
resolver.setPrefix("");
resolver.setSuffix(".vm");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
final String[] CLASSPATH_RESOURCE_LOCATIONS = [
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" ];
registry.addResourceHandler("/**").addResourceLocations(
CLASSPATH_RESOURCE_LOCATIONS);
}
}
Sujit Kamthe说的理由完全正确。我遇到了相同的错误,并通过在WebConfig类中手动配置“ContentNegotiationConfigurer”修复了该错误
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false).
favorParameter(false).
ignoreAcceptHeader(false).
useJaf(false).
defaultContentType(MediaType.TEXT_HTML).
mediaType("json", MediaType.APPLICATION_JSON);
}
}
这真的很奇怪,就我而言,我有30多个视图。将所有这些视图添加到此属性并在添加或删除视图时对其进行维护确实很痛苦。你确定这是解决问题所需要的吗?你解决问题了吗?我的日志上有相同的错误:-(