The image has been uploaded, but it doesn't appear on the screen! ??

trouble

Upload images using Spring Boot. Immediately after uploading, the image is not displayed on the HTML, but it is displayed when the screen is refreshed after waiting for about 30 seconds.

How to upload an image

Image file-> / resource / static / images / File name (file name such as xxx-20200712151409.jpg)-> Store in DB

Solution

  1. Create a folder (temporarily called images) directly under the root folder This time, images are created directly under the root folder called Ramen Kingdom. スクリーンショット 2020-07-12 15.17.29.png

  2. Create a WebConfiguration class (assumed to be AdditionalResourceWebConfiguration)

@Configuration
public class AdditionalResourceWebConfiguration implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**").addResourceLocations("file:images/");
    }
}

After that, just change the code that stored the images to images directly under the root folder. However, on HTML, you need to do the following:


<img th:src="@{'/images/' + ${picture.filepath}}"/>

Summary

Files that change dynamically like this time are not stored under static! !! !!

that's all. Thank you for reading to the end.

Recommended Posts

The image has been uploaded, but it doesn't appear on the screen! ??