Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Spring-boot static web resources location?

Base on this tutorial:

http://spring.io/guides/gs/serving-web-content/

I can use the thymeleaf to serve the view in the location

/src/main/resources/templates/

However, I have to put the static web contents (css, js) in another location:

/src/main/webapp/resources

And linking the resources in the hello.html like that:

<link href="resources/hello.css" />
<script src="resources/hello.js"></script>

The directory structure is:

└── src
    └── main
        └── java
            └── hello.java
        └──resources
            └──templates
               └──hello.html
        └──webapp
            └──resources
               └──hello.js
               └──hello.css

The problem is that the links of the static files are work when I run the web server. But if I open the html files in offline mode, the links are broken.

Could I move the static resource from

/src/main/webapp/resources

to:

/src/main/resources/templates/resources

The new directory structure would be like:

└── src
    └── main
        └── java
            └── hello.java
        └──resources
            └──templates
               └──hello.html
               └──resources
                  └──hello.js
                  └──hello.css

I tried it but is doesn't work.

like image 270
Tuan Avatar asked Mar 15 '14 07:03

Tuan


People also ask

How do I change the default static path in Spring Boot?

By default, Spring Boot serves all static content under the root part of the request, that is, /**. Even though it seems to be a good default configuration, we can change it via the spring.mvc.static-path-pattern configuration property.

How to add to existing static resource handlers in Spring Boot?

Springboot (via Spring) now makes adding to existing resource handlers easy. See Dave Syers answer. To add to the existing static resource handlers, simply be sure to use a resource handler path that doesn't override existing paths. The two "also" notes below are still valid.

What is resourcehttprequesthandler in Spring Boot?

2. Using Spring Boot 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 /static, /public, /resources, and /META-INF/resources directories that are on the classpath.

How do I change the default location of a spring resource?

Similar to path patterns, it's also possible to change the default resource locations via the spring.web.resources.static-locations configuration property. This property can accept multiple comma-separated resource locations:


1 Answers

Try src/main/resources/static (or src/main/resources/public or src/main/resources/resources). All those are registered by Spring Boot autoconfig.

like image 162
Dave Syer Avatar answered Sep 25 '22 10:09

Dave Syer