Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How add Static Web Content in Spring-boot

Tags:

spring-boot

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

like image 505
Alberto Crespo Avatar asked Mar 12 '15 19:03

Alberto Crespo


3 Answers

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

like image 181
Danilo Gomes Avatar answered Oct 09 '22 09:10

Danilo Gomes


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?

like image 34
jfcorugedo Avatar answered Oct 09 '22 07:10

jfcorugedo


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.

like image 9
Hamid Mohayeji Avatar answered Oct 09 '22 08:10

Hamid Mohayeji