Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload static content spring boot application

I am using Netbeans and I am developing my first web application using spring boot. I was keeping my HTML, js, CSS in "webapp" folder and then I refactored my project and I put all static content in /resources/static. Since then, I have to rebuild my project every time because the static content isn't reloaded.

Can I easily bypass this problem if I'll use browser-sync plugin for Gulp?

like image 527
Claude Avatar asked Apr 29 '17 08:04

Claude


People also ask

How does Spring Boot serve static content?

Spring Boot comes with a pre-configured implementation of ResourceHttpRequestHandler to facilitate serving static resources. By default, this handler serves static content from any of the /static, /public, /resources, and /META-INF/resources directories that are on the classpath.

Where do static files go in spring boot?

The file is located in the src/main/resources/static directory, which is a default directory where Spring looks for static content. In the link tag we refer to the main. css static resource, which is located in the src/main/resources/static/css directory. In the main.

What is static content in app?

Static content is anything that will not be executed as JSPs or Servlets. The following example is a basic HTML page that shows a welcome message.


2 Answers

Add the following to src/main/resources/application.properties:

spring.web.resources.static-locations[0]=file:src/main/resources/static/
spring.web.resources.static-locations[1]=classpath:/static/

The "file:" causes the content to be reloaded on refreshing the browser, see related issue.

Alternatively, the file resource locations can be discovered at runtime and added programmatically.

See also documentation and tutorial.

Note that prior to Spring Boot 2.4, the property was called "spring.resources.static-locations".

like image 67
AlexO Avatar answered Sep 28 '22 03:09

AlexO


Normally the static content is copied to the build directory ( target if you are using maven) by the spring-boot plugin. You can find your files at {build-directory}/classes/static: These are the files that you should modify to reflect changes. You should also copy your changes to resources/static, because whenever you restart spring boot, the files are copied.

like image 39
zakaria amine Avatar answered Sep 28 '22 04:09

zakaria amine