Spring Boot Thymeleaf 引入静态文件

基本引用

CSS

1
<link th:href="@{/layui/css/layui.css}" rel="stylesheet" type="text/css" href="../static/layui/css/layui.css" />

Javascript

1
<script th:src="@{/layui/layui.js}" type="text/javascript" charset="utf-8"></script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
spring:
thymeleaf:
cache: false
prefix: classpath:/templates/
suffix: .html
mode: HTML5
encoding: UTF-8
servlet:
content-type: text/html
resources:
static-locations: classpath:/static/
chain:
strategy:
content:
enabled: true
paths: /**

css/js引入失败解决

Spring Boot默认使用resources/static目录作为静态资源文件存放目录,若存在GlobalWebConfigWebMvcConfigurationSupport的配置类,则必须复写addResourceHandlers方法,否则引用css/js文件会报404错误。

1
2
3
4
5
6
7
8
9
@Configuration
public class GatewayConfig extends GlobalWebConfig {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
}

}
1
2
3
4
5
6
7
8
9
10
@Configuration
public class ResourceConfig extends WebMvcConfigurationSupport {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
super.addResourceHandlers(registry);
}

}

前置zuul网关后引用

CSS

1
<link th:href="@{/v1/appblog_service/layui/css/layui.css}" rel="stylesheet" type="text/css" href="../static/layui/css/layui.css" />

Javascript

1
<script th:src="@{/v1/appblog_service/layui/layui.js}" type="text/javascript" charset="utf-8"></script>

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :