My question is, what is the best practice to store images with spring boot and thymeleaf, if I want to load the image without the restart of the application. At the moment I'm able to upload the image so I see it in the uploaded folder and in the database I've stored It's name, But then when I refresh the page the Image is not loaded. I'm retrieving the image standard way in thymeleaf <img th:src="#{'../images/'+${product.image}}". The path is correct because after restart of the server the image is loaded.I guess there is a problem with static folder that It's included in jar and couldn't be changed. But what is the best practice to solve this kind of problem in spring boot. Thanks
The problem is the static directory is loaded at startup. So any changes files added after the ApplicationContext is finished. You probably would be interested in this post: Refreshing static content with Spring MVC and Boot I would look past Dave Syer's post as he is talking about the IDE only, but the answer below that by Steve should help you.
Related
I'm just starting to learn Spring reading "Spring In Action" and I cannot get my image to load on anything other than port 8080 (where tomcat launches). This would be fine normally, but I have to keep restarting the program to load any changes to my webpage.
Intellij tries to run my web program on port 63342, but no images load, only text. I believe this is an issue with Thymeleaf. I tried pointing tomcat to port 63342 but it won't load (port is in use?)
I would like it to where I can get the reload on save feature to work, and especially being able to use the shortcuts in intellij to launch the web page.
Any suggestions?
Basically the problem is, as I understand, that you're trying to open your .html template directly from IDEA. Which means you're not using the thymeleaf engine to render it - you're just opening the template itself and not the resulting page. If you open the source code of this page using your browser's dev tools, you will see that all your expressions stay unprocessed which leads to it trying to fetch the resources on the wrong port (e.g. 63342 instead of 8080 where your application is really running and is ready to serve the resources).
Thus, you cannot edit your templates like this. But Thymeleaf does have an option to do that. If you're running your Spring Boot application from the IDE directly (e.g. a profile in Intellij IDEA Ultimate) you can add the following to your application.properties:
spring.thymeleaf.cache=false
This will tell your Spring template resolver that you do not want to cache your .html templates and it will load the files from disk every time, so if you modify your template and then hit IDE's build button it should re-package your template and when you refresh your page in browser it will load (and process!) the edited one.
Caution! Only use this for development builds, set value to true for production environments to prevent unnecessary re-reading.
See Spring Boot Docs on Hot Swapping for more.
if you want intelj to display your image you have to add a regular src and add a th:src to show the image when the program is run. Try adding both
<img th:src="#{/assets/img/image.svg}" src="../static/assets/img/image.svg"/>
I have some CRUD app that connected through hibernate with the database. There is one JSP file in views. Everything is configured in the config XML file. But CSS still doesn't work.
My app running on IntelliJ IDEA 2019.1.2, MySql 8, Spring 5, Tomcat 9. I tried a few different ways to set up in JSP file path to CSS files. But the result always the same. Everything working, but without styles.
servlet config
project structure
jsp file
result
In the png you it seem you are using resourses instead of resources. Also verify in developer tool of your browser the name of resources folder. You can go to sources tab(chrome) or debugger(firefox) and check the folder structure.
I have the following problem: I haven an application which uses SpringMVC and Hibernate, and when the user uploads an image it stores it in the /resources/img folder, but when I have to do another deploy I complile my source code into a war, and then I deploy it again, overwriting the image that the user has just upload becose the whole folder is in the war. I don't know how it works, but the problem is that the user lose every image that he has uploaded when I deploy the app again.
Does anyone know the best way to solve this? I know that save the image in the database is not a good practice, so I want to avoid that.
Thank you!
I want use cache control in spring mvc for static files.
I have gone through the following scenarios
Using WebContentHandlerInterceptor.
Using browser cache headers.
Using mvc:resources
Version number/build number for the js files.
But my problem is when the user comes for the first time it is loaded with the latest static files. If I update any JS files that are needed to be updated in the test or production server before the cache expiration. The browser is taking only from cache until I reload using F5 or Ctrl+F5.
When the user is requested a page, then all static files are to be checked and if they are not modified then it has to use cache otherwise take the latest one from the server.
Please help me.. I am newbee to this stackoverflow.
To ensure browser to download the latest version of your static files is to add a parameter to the URL.
For example, your request will look like resources/scripts/menu.js?ver=1.0.
I am getting Error 404 when trying to run my application.
Tomcat server, start and synchronized. Dynamic module version is 2.5.
Previously I used dynamic module version 3.o but it didn't work. I read in
one of the posts that better is to use 2.5. so I created new project and
still get this 4044 error. It is technical error I think.
I needed to create index.jsp WebContent->New->JSP file and it will place it in the right spot. Now I am able to see my index page.
The problem was that I created index.jsp file WEB-INF->New->JSP file. You can see it above in my post.
Here I place the image where you can see image.jsp is place in different spot. You can see small difference.
That's why I asked you to create a test dynamic project to compare. I think when you created that new project you placed index.html under WebContent correctly. So you couldn't figure out with that example.
Also you can place your jsp inside WEB-INF. That's a recommended way too to keep it safe . I mean if the file is inside WebContent a Web user can navigate to the location and access it. But if it is inside WEB-INF the access is controlled by your web.xml.
So in your former case just give the relative path .ie /WEB-INF/page.jsp