Spring Boot Thymeleaf 引入静态文件

基本引用

CSS

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

Javascript

<script th:src="@{/layui/layui.js}" type="text/javascript" charset="utf-8"></script>
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错误。

@Configuration
public class GatewayConfig extends GlobalWebConfig {

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

}
@Configuration
public class ResourceConfig extends WebMvcConfigurationSupport {

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

}

前置zuul网关后引用

CSS

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

Javascript

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

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/11/spring-boot-thymeleaf-introduces-static-files/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Spring Boot Thymeleaf 引入静态文件
基本引用 CSS <link th:href="@{/layui/css/layui.css}" rel="stylesheet" type="text/css" href="../static/layui/c……
<<上一篇
下一篇>>
文章目录
关闭
目 录