Spring 静态资源映射

  1.  开启@EnableWebMvc注解,若没有该注解,重写WebMvcConfigurerAdaper方法则无效。
  2. addResourceHandler指定对外暴露的访问路径,addResourceLocation指定文件放置目录。

配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @author Kevin
 * @description
 * @date 2016/7/4
 */
@Configuration
@EnableWebMvc
@ComponentScan("ch04.web")
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // addResourceHandler指定对外暴露的访问路径,addResourceLocation指定文件放置目录
        registry.addResourceHandler("/assets").addResourceLocations("classpath:/assets/");
    }
}

 

转载于:https://my.oschina.net/kevinair/blog/705077

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,静态资源映射是由Spring Boot默认提供的配置完成的。Spring Boot将所有访问映射到以下目录:classpath:/static、classpath:/public、classpath:/resources和classpath:/META-INF/resources。如果这些目录中有多张同名的图片,访问地址的优先级顺序为:META-INF/resources > resources > static > public。\[2\] 如果我们请求一个以".html"结尾的静态资源Spring Boot会按照优先级高的文件夹先查找,然后再查找优先级低的文件夹,直到找到指定的静态资源为止。\[3\] 例如,在src/main/resources/static目录下创建了一个hello.html文件,当我们启Spring Boot并访问"http://localhost:8080/hello.html"时,就会显示该hello.html页面。\[3\] 总结来说,Spring Boot对静态资源映射提供了默认配置,可以直接将静态资源放置在指定的目录中,无需进行额外的配置即可访问这些静态资源。\[1\] #### 引用[.reference_title] - *1* *3* [Spring Boot静态资源映射](https://blog.csdn.net/weixin_64842782/article/details/125089662)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [springboot静态资源映射](https://blog.csdn.net/ChOLg/article/details/102800978)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值