Spring can not load CSS
Spring can not load CSS
Hello I have a little problem, when i'm starting my Spring boot web app, when i'm going to my localhost it cant load CSS.
I figured out that it's happening after adding "@ComponentScan" into Main class.
So i tried to remove it and then my whole controllers couldnt load ...
Meybe someone know solution to this problem. I've tried for about 3 days to do it and nothing helped. I'm putting a code below. meybe someone could figre it out for me.
MAIN.class
@SpringBootApplication
@EntityScan({"com.estomed.Model"})
@EnableJpaRepositories({"com.estomed.Repository"})
@ComponentScan(basePackages = {"com.estomed"}, useDefaultFilters = true)
public class SynchronizesiteApplication {
public static void main(String args) {
SpringApplication.run(SynchronizesiteApplication.class, args);
}
}
WebConfig.class
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
registry.addResourceHandler(
"/webjars/**",
"/img/**",
"/css/**",
"/js/**")
.addResourceLocations(
"classpath:/static/webjars/",
"classpath:/static/img/",
"classpath:/static/css/",
"classpath:/static/js/");
}
}
Header.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:fragment="header">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="shortcut icon" type="image/x-icon" th:src="@{~/images/logo2.png}"
/>
<link th:text="@{~/css/bootstrap.min.css}" rel="stylesheet" />
<link th:text="@{~/style.css}" rel="stylesheet" />
</head>
<body> <!-- Navigation -->
<!--class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top"-->
</body>
</html>
Project structure
Error
I dont know what can i do more/I hope guys you'd help me
2 Answers
2
You have to use th:href
instead of th:text
for link tag
th:href
th:text
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:fragment="header">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="shortcut icon" type="image/x-icon" th:href="@{/images/logo2.png}"
/>
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet" />
<link th:href="@{/style.css}" rel="stylesheet" />
</head>
<body> <!-- Navigation -->
<!--class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top"-->
</body>
</html>
Try using
<link th:href="@{/bootstrap.min.css}" href="/bootstrap.min.css" rel="stylesheet" type="text/css" />
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I did that and still nothing, i even tried do something like : <link type="text/css" rel="stylesheet" th:href="@{~/css/style.css}" /> still doesnt work
– Carlton Leatch
Jun 29 at 18:15