I was trying to use auto configured serving static content in spring boot.
I read it is enough to put that content to /static/
or /resources/
and configure on @Controller
in order to do it.
In my project it looks like it does not work at all.
I have compared it with spring-boot-sample-web-ui
and the only thing I have different is packaging. There is jar
and I have war
packaging.
Could you please confirm me that in spring-boot in war
packaging this auto configured static content serving does not work?
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.
There are three steps to requesting static content from a server: A user sends a request for a file to the web server. The web server retrieves the file from disk. The web server sends the file to the user.
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.
First create a folder named static
in src/main/resources
.
Now if you want something to be displayed by default you can add an index.html to that folder and you can display that as your landing page (Ex: http://localhost:8080/my-app-name
)
Then, if you have other content you can create folders by type put those inside.
Ex:
images -> src/main/resources/static/images
js -> src/main/resources/static/js
If you put login.js
file inside above js
folder, you can access it now from the url http://localhost:8080/my-app-name/js/login.js
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With