It's okay to upload the image with the web app I'm creating, but I'm stuck with the problem that the uploaded image is not updated on the screen, so I'll make a note for myself. It seems that the cause is that the cache is bad and the image before update is read as it is.
From the conclusion, I was able to solve it by adding? (Some value) to "<img src = ~" of the image displayed on the jsp side and letting the browser recognize it as a different image. ..
test.jsp
//Change before
<img src="static/img/test.jpg " alt="test.jpg ">
//After change
<img src="static/img/test.jpg?(Any dynamic parameter)" alt="test.jpg ">
If the URL of the image is exactly the same and nothing is changed, the browser will judge ** "Use the cache" **, so it seems that the update was not reflected.
Since it is an arbitrary unique parameter, anything is fine, but since it is a big deal, I will try to get the value automatically and give it. Get the uploaded date and time with lastModified and give it.
Controller.java
//Select the target file
File file = new File("static/img/test.jpg ");
//Get the last modified date and time of the file and make it available in jsp
model.addAttribute("file", file.lastModified());
test.jsp
<img src="/static/img/test.jpg?${file}" alt="test.jpg ">
Now when you update the file, the updated one will be loaded.
-Spring Boot supports browser cache of static resources ・ [Don't say "it's because of the cache"! How to make sure that only updated files have their cache disabled] (https://qiita.com/laineus/items/ae9c9117cbcbd5333035) -[Discard the browser cache and load only updated JS, CSS, and images] (https://qiita.com/ka215/items/20139918642fed39fb5d)
Recommended Posts