Reputation: 5611
My project tree looks like this:
I can access the templates now, but can't load static resources such as CSS, images, and JS.
I have a common.html
fragment where I declare all my static resources, for instance:
<link rel="stylesheet" th:href="@{/css/app.css}"/>
A header fragment where I include common.html
like so:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head th:include="fragments/common :: commonFragment" lang="en"></head>
// page body
</html>
A default.html
file for layout:
<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.thymeleaf.org"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head th:include="fragments/common :: commonFragment" lang="en">
<meta charset="utf-8"/>
<meta name="robots" content="noindex, nofollow"/>
<title>Touch</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta http-equiv="content-dataType" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="apple-mobile-web-app-capable" content="yes"/>
<link rel="shortcut icon" th:href="@{/static/images/favicon.ico}" type="image/x-icon" />
</head>
<body>
<div th:id="defaultFragment" th:fragment="defaultFragment" class="container">
<div id="header" th:replace="fragments/header :: headerFragment" />
<div layout:fragment="content" />
</div>
</body>
</html>
Also, on my application.properties
file, I have these entries:
#SPRING RESOURCE HANDLING
spring.resources.static-locations=classpath:/resources/
#THYMELEAF
spring.thymeleaf.cache = true
spring.thymeleaf.check-template = true
spring.thymeleaf.check-template-location = true
spring.thymeleaf.content-type=text/html
spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.prefix=classpath:templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.template-resolver-order=1
But I keep getting the same No mapping found
message.
32190 [http-nio-8081-exec-2] WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/central/css/app.css] in DispatcherServlet with name 'dispatcherServlet'
What am I not seeing here?
Upvotes: 3
Views: 9629
Reputation: 15230
I'm using Spring Boot 2.2 and not getting any of my static content. I discovered two solutions that worked for me:
Option #1 - Stop using @EnableWebMvc
annotation
This annotation disables some automatic configuration, including the part that automatically serves static content from commonly-used locations like /src/main/resources/static
. If you don't really need @EnableWebMvc
, then just remove it from your @Configuration
class.
Option #2 - Implement WebMvcConfigurer
in your @EnableWebMvc
annotated class and implementaddResourceHandlers()
Do something like this:
@EnableWebMvc
@Configuration
public class SpringMVCConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");
registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/");
registry.addResourceHandler("/vendor/**").addResourceLocations("classpath:/static/vendor/");
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
}
}
Just remember that your code is now in charge of managing all static resource paths.
Upvotes: 4
Reputation: 126
I solved this problem with the following code:
spring.resources.static-locations=classpath:templates/
Upvotes: 5
Reputation: 97
try to add mapping of your static resources in link
<link rel="stylesheet" th:href="@{/css/app.css}"
href="../../../css/app.css" />
Upvotes: 0
Reputation: 21391
Use the spring.resources.static-locations in your properties to define the static resources locations (making them directly publicly available):
spring.resources.static-locations=classpath:/your/static/resources/here/like/central/css/
you can provide comma separated values i.e. taken from the documentation:
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ # Locations of static resources.
Upvotes: 3