springboot之templates静态资源处理

Springboot之静态资源处理

thymeleaf

1. 引入依赖

<!--thymeleaf 模板依赖-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2. yml配置

spring:
  thymeleaf:
    prefix: classpath:/templates/
    cache: false
    suffix: .html
  mvc:
  	# 这里配置的pathPatterns和引入的js要一致
    static-path-pattern: /**
  resources:
    static-locations: classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/public/, classpath:/templates/

3. java配置访问

//1.直接在启动类加
@SpringBootApplication
public class AppApplication implements WebMvcConfigurer {

    public static void main(String[] args) {
        SpringApplication.run(AppApplication.class, args);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
       registry.addResourceHandler("/**").  //表示文件路径,这里的意思是项目包下的所有文件
                addResourceLocations("classpath:/static/"). //表示要开放的资源
                addResourceLocations("classpath:/resources/").
                addResourceLocations("classpath:/public/").
           		addResourceLocations("classpath:/META-INF/resources/");  //swagger2页面;
				.addResourceLocations("file:D:/ZFSOFT_ZWFW/apache-tomcat-8.5.49-app/bin/static/"); //媒体资源
        // resourceLocations里面的值可以都配置在yml的spring.resources.static-locations中
        super.addResourceHandlers(registry);
    }
}

// 在配置类中加
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    @Value("spring.resources.static-locations")
    private String resourceLocations;
    @Value("spring.resources.static-path-pattern")
    private String pathPatterns;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(pathPatterns).
                addResourceLocations(resourceLocations);
        super.addResourceHandlers(registry);
    }
}

4. 通过请求访问

@Controller
@RequestMapping("/uPay")
public class UpayController {

    @RequestMapping("/upayTest")
    public String upayTest() {
        // 访问upayTest.html文件
        return "upayTest";
    }
}

5. 引入js

// 如果static-path-pattern: /static/** src="static/js/test.js"
<script type="text/javascript" src="js/test.js"></script>

6. 文件路径图

在这里插入图片描述

外部静态资源

@Component
public class WebMvcConfig implements WebMvcConfigurer {
    @Resource
    private BaseInterceptor baseInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(baseInterceptor)
                //需要配置2:----------- 告知拦截器:/static/admin/** 与 /static/user/** 不需要拦截 (配置的是 路径)
                .excludePathPatterns("/static/admin/**", "/static/user/**");
    }

    /**
     * 添加静态资源文件,外部可以直接访问地址
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //其他静态资源
        registry.addResourceHandler("/upload/**").addResourceLocations("file:"+ TaleUtils.getUplodFilePath()+"upload/");

        //需要配置1:----------- 需要告知系统,这是要被当成静态文件的!
        //第一个方法设置访问路径前缀,第二个方法设置资源路径
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //registry.addViewController("/error/404").setViewName("/admin/page_error/error_404.html");
    }
}
# 应该以什么样的路径来访问静态资源,这表示只有静态资源的访问路径为/static/ 时才会处理(如http://localhost:8080/static/css/base.css)
spring.mvc.static-path-pattern: /static/**
#用于告诉Spring Boot应该在何处查找静态资源文件,查找文件时会依赖于配置的先后顺序依次进行
spring.resources.static-locations=classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources


想要理解这两个配置的具体区别,参考: SpringBoot中配置Web静态资源路径——详解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值