Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot: serving static content with war packagin

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?

like image 599
firen Avatar asked Aug 11 '14 08:08

firen


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.

How is static content served?

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.

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.


1 Answers

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

like image 164
Maleen Abewardana Avatar answered Oct 09 '22 22:10

Maleen Abewardana