I've been trying to get this working but no luck after several hours.
My structure is like this:
š src
āāāā š main
āāāā š java
āāāā š resources
āāāā š META-INF
ā āāāā š resources
ā āāāā š bootstrap
ā āāāā š css
ā āāāā š js
āāāā š templates
ā āāāā index.html
āāāā application.properties
Here is the WebConfig class:
@Configuration
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
}
If I use link to a web resource, it works:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"></link>
If I'm trying to include a static resource, it doesn't work:
<link type="text/css" rel="stylesheet" href="../../../resources/bootstrap/css/bootstrap.min.css"
data-th-href="@{/resources/bootstrap/css/bootstrap.min.css}" />
Any help is highly appreciated.
Thank you
Do not use the
src/main/webappdirectory if your application will be packaged as a jar. Although this directory is a common standard, it will only work with war packaging and it will be silently ignored by most build tools if you generate a jar.
/src/main/resources/): By default Spring Boot will serve static content from a directory called
/static(or/publicor/resourcesor/META-INF/resources).
I suggest you reorganize your webapp files into /src/main/resources/static
When accessing static files, the static folders (listed above) are the path's root.
Thus, to access your css file:
META-INF/resources/bootstrap/css/bootstrap.min.csshttp://localhost:8080/bootstrap/css/bootstrap.min.css<link type="text/css" rel="stylesheet" href="bootstrap/css/bootstrap.min.css"/><link type="text/css" rel="stylesheet" data-th-href="@{bootstrap/css/bootstrap.min.css}" />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