22

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:

enter image description here

UPDATED 2: trying the @jfcorugedo

Doesnt work. Take a look, is that what was you saying?

enter image description here

UPDATED 3:trying the @jfcorugedo, second advice:

enter image description here

Alberto Crespo
  • 2,429
  • 5
  • 28
  • 51

4 Answers4

21

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:

enter image description here

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).

Danilo Gomes
  • 571
  • 6
  • 13
19

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:

folder hierarchy in my test project

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?

jfcorugedo
  • 9,793
  • 8
  • 39
  • 47
  • so the absolute path on the server for that is: /project/src/main/META-INF/ ? or just /project/META-INF ? – Alberto Crespo Mar 13 '15 at 12:49
  • 3
    You have to use this one: src/main/resources/META-INF/ Don't put resources inside src/main, because maven, by default, only recognises files inside src/main/java and src/main/resources – jfcorugedo Mar 13 '15 at 13:01
  • I tried too without success. The server gives me an 404 as always... My knowledge about Spring-boot is very poor but , maybe this information help us: I have built the project with Gradle, I use TomCat 7 and I run that as a JavaApplication. – Alberto Crespo Mar 13 '15 at 13:38
  • Just a note that "static" did not work for me.......but "resources" did......I"m talking about the 2nd "resources"..aka, the more deeply nested one. – granadaCoder Aug 22 '18 at 22:05
9

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.

Hamid Mohayeji
  • 3,977
  • 3
  • 43
  • 55
1

Create a folder anywhere with a /static folder on the root. This can be anywhere on your work space. Example C:\mywebapp\static. Add C:\mywebapp to the runtime classpath, or if using Intellij/Eclipse, add the folder C:\mywebapp as a library.

This is different than adding your static html as a source folder, which will cause it be be copied to the output class folder with every build. If you have a large web app or single page app using angular or sencha, this is a pain. Instead add the folder C:\mywebapp as a library folder or run time classpath folder.

The benefit of this is it will allow your javascript code to run on the same host/port as your spring boot rest,ajax, etc services without having to deal with jsonp or cors.

Dan Poulos
  • 41
  • 2