ERROR org.apache.velocity: ResourceManager: cannot find resource "xxx.html.vm" in any resource loader

I use Velocity templates along with Spring boot.

When there is a file named "xxx.vm" in the templates directory, Spring Boot successfully loads "xxx.vm". But the ERROR message below is logged.

"ERROR org.apache.velocity: ResourceManager: cannot find the resource" xxx.html.vm "in any resource loader.

I do not understand why the system is looking for "xxx.html.vm" because the suffix in application.properties is set to ".vm"

Here is the configuration in application.properties

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

There is no problem launching my application, but I would like to know what causes this error message. Could you help me deal with this? Thank you in advance.

+5
source share
3 answers

Add the following line to application.properties :

 spring.velocity.view-names=xxx,yyy,zzz 
+3
source

This is because spring boot sets up various ViewResolvers based on what is available in the classpath. If a speed dependency is found in the classpath, spring will configure VelocityViewResolver, but at the same time it also sets up other resolving views, ContentNegotiationViewResolver is one of them.

ContentNegotiationViewResolver tries to match the view name and MIME type to automatically determine the best view. In this process, he tries to find XXX.vm.html and therefore throws an exception.

To fix this, adjust your viewing permissions manually. See http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-switch-off-default-mvc-configuration

I set up my viewResolvers manually by introducing the following class, and the problem went away.

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

The reason @Sujit Kamthe said exactly right. I got the same error and fixed it by manually setting the "ContentNegotiationConfigurer" in the WebConfig class.

 @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

0
source

Source: https://habr.com/ru/post/1241459/


All Articles