简体   繁体   English

错误org.apache.velocity:ResourceManager:无法在任何资源加载器中找到资源'xxx.html.vm'

[英]ERROR org.apache.velocity : ResourceManager : unable to find resource 'xxx.html.vm' in any resource loader

I'm using Velocity templates along with Spring boot. 我正在使用Velocity模板和Spring启动。

When there is a file named 'xxx.vm' in templates directory, Spring Boot successfully loads 'xxx.vm'. 当模板目录中有一个名为“xxx.vm”的文件时,Spring Boot会成功加载“xxx.vm”。 But an ERROR message below is logged. 但是会记录下面的ERROR消息。

"ERROR org.apache.velocity : ResourceManager : unable to find resource 'xxx.html.vm' in any resource loader." “错误org.apache.velocity:ResourceManager:无法在任何资源加载器中找到资源'xxx.html.vm'。”

I don't understand why the system looks for 'xxx.html.vm' because the suffix in the application.properties is set to ".vm" 我不明白为什么系统会查找'xxx.html.vm',因为application.properties中的后缀设置为“.vm”

Here is configuration in application.properties 这是application.properties中的配置

spring.velocity.enabled=true
spring.velocity.resource-loader-path=classpath:/templates/
spring.velocity.suffix=.vm

There is no problem with running my application, but I'd like to know what causes this Error message. 运行我的应用程序没有问题,但我想知道导致此错误消息的原因。 Could you please help me deal with this? 你能帮我解决这个问题吗? Thank you in advance. 先感谢您。

将以下行添加到application.properties

spring.velocity.view-names=xxx,yyy,zzz

This happens because spring boot configures various ViewResolvers based on what is available in classpath If velocity dependency is found in classpath, then spring would configure a VelocityViewResolver, but along with that it configures other view resolvers too, ContentNegotiatingViewResolver being one of them. 这是因为Spring引导根据类路径中可用的内容配置各种ViewResolvers如果在类路径中找到了速度依赖性,那么spring将配置VelocityViewResolver,但同时它也配置其他视图解析器,ContentNegotiatingViewResolver就是其中之一。

ContentNegotiatingViewResolver tries to match the view name and MIME type to determine the best possible view automatically. ContentNegotiatingViewResolver尝试匹配视图名称和MIME类型,以自动确定最佳视图。 In this process it tries to find the XXX.vm.html and hence throws the exception. 在此过程中,它尝试查找XXX.vm.html,从而抛出异常。

To fix this configure your view resolvers manually. 要解决此问题,请手动配置视图解析程序。 Refer: http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-switch-off-default-mvc-configuration 请参阅: http//docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-switch-off-default-mvc-configuration

I did configure my viewResolvers manually by introducing following class, and the problem disappeared. 我通过引入以下类来手动配置我的viewResolvers,问题就消失了。

@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);
}
}

The reason @Sujit Kamthe said exactly correct. @Sujit Kamthe之所以说完全正确。 I hava the same error and fixed it by configuring "ContentNegotiationConfigurer" manually in the WebConfig class. 我有同样的错误并通过在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);
    }
}

Refer:https://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc 请参阅:HTTPS://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误[org.apache.velocity] ResourceManager:无法在任何资源加载器中找到资源“ layout.vm” - ERROR [org.apache.velocity] ResourceManager : unable to find resource 'layout.vm' in any resource loader ResourceManager:无法在任何资源加载器中找到资源'emailTemplate.vm' - ResourceManager : unable to find resource 'emailTemplate.vm' in any resource loader ResourceManager:无法在任何资源加载器中找到资源“index.vm” - ResourceManager : unable to find resource 'index.vm' in any resource loader 在任何资源加载器中获取速度错误“无法找到资源'error.vm'。” - Getting velocity error “unable to find resource 'error.vm' in any resource loader.” Spring + Thymeleaf:ResourceManager:无法在任何资源加载器中找到资源“ index.vm” - Spring + Thymeleaf: ResourceManager : unable to find resource 'index.vm' in any resource loader org.apache.velocity.exception.ResourceNotFoundException:无法找到资源“模板/电子邮件/test.vm” - org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'templates/email/test.vm' Velocity模板-线程“ main”中的异常org.apache.velocity.exception.ResourceNotFoundException:无法找到资源 - Velocity Template - Exception in thread “main” org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource Apache Android 中的速度(“无法找到资源”) - Apache Velocity in Android("unable to find resource") Spring Boot / Velocity无法找到资源错误 - Spring Boot/Velocity Unable to find resource Error 将另一个vm文件包含到速度模板中时找不到资源 - Unable to find resource while include another vm file into velocity template
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM