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.
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.
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.
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.
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:
Try src/main/resources/static
(or src/main/resources/public
or src/main/resources/resources
). All those are registered by Spring Boot autoconfig.
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