Thymeleaf + Spring-Boot - 为什么我不能访问静态资源?
Thymeleaf + Spring-Boot - why can't I access static resources?
我的项目树是这样的:
我现在可以访问模板,但无法加载静态资源,例如CSS、图片和JS。
我有一个 common.html
片段,我在其中声明了所有静态资源,例如:
<link rel="stylesheet" th:href="@{/css/app.css}"/>
一个 header 片段,我在其中包含 common.html
,如下所示:
<!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>
一个 default.html
布局文件:
<!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>
此外,在我的 application.properties
文件中,我有这些条目:
#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
但我一直收到相同的 No mapping found
消息。
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'
我在这里没有看到什么?
在您的属性中使用 spring.resources.static-locations 来定义静态资源位置(使其直接公开可用):
spring.resources.static-locations=classpath:/your/static/resources/here/like/central/css/
您可以提供逗号分隔值,即 taken from the documentation:
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ # Locations of static resources.
尝试在link
中添加静态资源的映射
<link rel="stylesheet" th:href="@{/css/app.css}"
href="../../../css/app.css" />
我用下面的代码解决了这个问题:
spring.resources.static-locations=classpath:templates/
我正在使用 Spring Boot 2.2,但没有获得任何静态内容。我发现了两个对我有用的解决方案:
选项 #1 - 停止使用 @EnableWebMvc
注释
此注释禁用了一些自动配置,包括自动提供来自 /src/main/resources/static
等常用位置的静态内容的部分。如果您真的不需要 @EnableWebMvc
,那么只需将其从 @Configuration
class.
中删除即可
选项 #2 - 在您的 @EnableWebMvc
注释 class 中实施 WebMvcConfigurer
并实施 addResourceHandlers()
做这样的事情:
@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/");
}
}
请记住,您的代码现在负责管理所有静态资源路径。
我的项目树是这样的:
我现在可以访问模板,但无法加载静态资源,例如CSS、图片和JS。
我有一个 common.html
片段,我在其中声明了所有静态资源,例如:
<link rel="stylesheet" th:href="@{/css/app.css}"/>
一个 header 片段,我在其中包含 common.html
,如下所示:
<!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>
一个 default.html
布局文件:
<!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>
此外,在我的 application.properties
文件中,我有这些条目:
#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
但我一直收到相同的 No mapping found
消息。
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'
我在这里没有看到什么?
在您的属性中使用 spring.resources.static-locations 来定义静态资源位置(使其直接公开可用):
spring.resources.static-locations=classpath:/your/static/resources/here/like/central/css/
您可以提供逗号分隔值,即 taken from the documentation:
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ # Locations of static resources.
尝试在link
中添加静态资源的映射<link rel="stylesheet" th:href="@{/css/app.css}"
href="../../../css/app.css" />
我用下面的代码解决了这个问题:
spring.resources.static-locations=classpath:templates/
我正在使用 Spring Boot 2.2,但没有获得任何静态内容。我发现了两个对我有用的解决方案:
选项 #1 - 停止使用 @EnableWebMvc
注释
此注释禁用了一些自动配置,包括自动提供来自 /src/main/resources/static
等常用位置的静态内容的部分。如果您真的不需要 @EnableWebMvc
,那么只需将其从 @Configuration
class.
选项 #2 - 在您的 @EnableWebMvc
注释 class 中实施 WebMvcConfigurer
并实施 addResourceHandlers()
做这样的事情:
@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/");
}
}
请记住,您的代码现在负责管理所有静态资源路径。