I am trying to add static web content in a spring-boot server using the info here
I have tried adding the .js and .css files I need in the several Folders that the previous link says but it doesn't work:
/META-INF/resources/ /resources/
Note: I didn't created the /static/
and /public/
folders because I don't know the absolute Path in my project.
I have added too the addResourceHandlers
method:
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { if (!registry.hasMappingForPattern("/webjars/**")) { registry.addResourceHandler("/webjars/**").addResourceLocations( "classpath:/META-INF/resources/webjars/"); } if (!registry.hasMappingForPattern("/**")) { registry.addResourceHandler("/**").addResourceLocations( RESOURCE_LOCATIONS); } }
And the reference in the HTML file are like this:
<script src="bootstrap-switch.js"></script>
Any idea how I can fix that?
UPDATED:
UPDATED 2: trying the @jfcorugedo
Doesnt work. Take a look, is that what was you saying?
UPDATED 3:trying the @jfcorugedo, second advice:
I think you should access static contents using mydomain/<context>/<resource type>/resource
For instance:
http://mydomain/app/js/test.js
http://mydomain/app/img/test.jpg
You can write your resources using application context or simply using relative path:
<script src="js/bootstrap-switch.js"></script>
or
<script src="/<myapp>/js/bootstrap-switch.js"></script>
But if you are using bootstrap with a webjar you should write
<script src="/<myapp>/webjars/bootstrap/<version>/js/bootstrap-switch.js"></script>
There is a project on github to illustrate this https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-static
UPDATE 1
I created a test project with the follow structure:
I could access style.css on http://localhost:8080/css/style.css
You can use http://start.spring.io/ to help you create new projects
Another option is use Spring Tool Suite or IntelliJ IDEA. They can help you to create new project with right configurations (as the previous link).
You don't need to add the method addResourceHandlers, Spring boot executes this method in its own code.
Don't put the files inside webapp folder.
You only need to put your files in one of these directories:
/META-INF/resources/
/resources/
/static
/public
For instance, if you want to load the file js/bootstrap-switch.js
, this file must be in one of those folders:
/META-INF/resources/js/bootstrap-switch.js
/resources/js/bootstrap-switch.js
...
I've created a folder META-INF/resources/js inside my /src/main/resources and everything works:
I've put a test.js file inside this folder and, when I've type the URL http://localhost:8080/js/test.js the application has given me the JS file.
Are you sure you're creating this same folder hierarchy?
Looking at org.springframework.boot.autoconfigure.web.ResourceProperties
class, you can see this line of code:
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
Meaning /META-INF/resources/
, /resources/
, static/
and public/
directories are available to serve static contents.
So you can create a static/
or public/
directory under resources/
directory and put your static contents there. And they will be accessible by: http://localhost:8080/your-file.ext
. (assuming the server.port
is 8080)
You can customize these directories using spring.resources.static-locations
in the application.properties
.
For example by providing this configuration:
spring.resources.static-locations=classpath:/custom/
You can use custom/
folder under resources/
for serving static files and access them through the URL above.
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