簡體   English   中英

錯誤org.apache.velocity:ResourceManager:無法在任何資源加載器中找到資源'xxx.html.vm'

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

我正在使用Velocity模板和Spring啟動。

當模板目錄中有一個名為“xxx.vm”的文件時,Spring Boot會成功加載“xxx.vm”。 但是會記錄下面的ERROR消息。

“錯誤org.apache.velocity:ResourceManager:無法在任何資源加載器中找到資源'xxx.html.vm'。”

我不明白為什么系統會查找'xxx.html.vm',因為application.properties中的后綴設置為“.vm”

這是application.properties中的配置

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

運行我的應用程序沒有問題,但我想知道導致此錯誤消息的原因。 你能幫我解決這個問題嗎? 先感謝您。

將以下行添加到application.properties

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

這是因為Spring引導根據類路徑中可用的內容配置各種ViewResolvers如果在類路徑中找到了速度依賴性,那么spring將配置VelocityViewResolver,但同時它也配置其他視圖解析器,ContentNegotiatingViewResolver就是其中之一。

ContentNegotiatingViewResolver嘗試匹配視圖名稱和MIME類型,以自動確定最佳視圖。 在此過程中,它嘗試查找XXX.vm.html,從而拋出異常。

要解決此問題,請手動配置視圖解析程序。 請參閱: http//docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-switch-off-default-mvc-configuration

我通過引入以下類來手動配置我的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);
}
}

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

請參閱:HTTPS://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM