I am doing a project with spring MVC and Thymeleaf. I have a question about how I should reference my CSS files if I have this folder structure:
src
main
webapp
resources
myCssFolder
myCssFile.css
web-inf
spring
views
myViewFolder
index.html
My configuration class is like this:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/css/**");
registry.addResourceHandler("/img/**").addResourceLocations("/img/**");
registry.addResourceHandler("/js/**").addResourceLocations("/js/**");
registry.addResourceHandler("/sound/**").addResourceLocations("/sound/**");
registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/**");
}
And I call href
in my index file like this:
href="resources/css/bootstrap.min.css"
But there are some elements that are kind of messed up in my page, for example the CSS is not working.
I have such problem!That steps helped me.
<link th:href="@{/css/MyCSS.css}" href="/css/MyCSS.css" rel="stylesheet" type="text/css" />
You will need to use
th:href
attribute for referring css files. Here is a sample from thymeleaf tutorial. If thymeleaf can not evaluateth:href
value, it defaults tohref
value.