简体   繁体   中英

Problem loading images in Spring Boot / FreeMarker

I have been working on a demonstration application to understand FreeMarker templates with Spring Boot. I like FreeMarker templates, but I can't get images to display on the web pages. I have tried everything I can think of as far as the placement of the image and image directory, but nothing has worked. I hope that someone can point out my problem, since if I can't solve this issue, I can't use FreeMarker.

My project directory looks like:

Spring Boot项目目录

I am using Spring Boot 2.1.1.

I use Bootstrap for page formatting. Here is the Bootstrap/HTML that references the image:

  <div class="row"> <div class="col-md-12"> <img src="/img/snowy_egret_thumb.jpg" /> </div> </div> <!-- row --> 

I have thought that perhaps I am missing something in the FreeMarker configuration, but I have not found anything. My FreeMarker Configuration class is shown below:

@Configuration
@EnableWebMvc
@ComponentScan({"cognitodemo.freemarker"})
public class AppConfig implements WebMvcConfigurer,         
ApplicationContextAware {
   private ApplicationContext applicationContext = null;


   @Override
   public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
      this.applicationContext = applicationContext;
  }


@Bean
@Description("FreeMarker View Resolver")
public FreeMarkerViewResolver viewResolver(){
    FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
    viewResolver.setCache(false);
    viewResolver.setPrefix("");
    viewResolver.setSuffix(".html");
    return viewResolver;
}

@Bean
public FreeMarkerConfigurer freemarkerConfig() {
    FreeMarkerConfigurer freeMarkerConfigurer = new 
FreeMarkerConfigurer();
    freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/views/");
    return freeMarkerConfigurer;
}

}

When I run the application with Spring Boot I get the following warning:

WARN[0;39m [35m13617[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.sbafFreeMarkerAutoConfiguration [0;39m [2m:[0;39m Cannot find template location(s): [classpath:/templates/] (please add some templates, check your FreeMarker configuration, or set spring.freemarker.checkTemplateLocation=false)

However, the application pages work properly. It's just that the image will not load.

Any help would be greatly appreciated. Many thanks in advance.

After lots of hacks, I have finally gotten the image loading to work. What made the difference was adding an addResourceHandler() method to the configuration class AppConfig. This method is shown below:

public void addResourceHandlers(ResourceHandlerRegistry registry) {  
    registry.addResourceHandler("/img/**")
            .addResourceLocations("/img/")
            .addResourceLocations("classpath:/img/"); 
 }

I also moved the image directory so that it was under webapp. This is shown below:

在此处输入图片说明

I also added the line

spring.freemarker.checkTemplateLocation: false

to my application.properties file.

Note that the resources directory is still under the views directory. I don't see this as ideal, since I would rather move it to the same level as the img directory. I tried to add code to the resource handler for this, but I didn't succeed. What I have works, so I'm going to go with this.

I have published this application on GitHub. See https://github.com/IanLKaplan/CognitoDemoFreeMarker

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM