添加静态文件

第一种,使用property文件

1
2
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/static/

第二种,使用自定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
* 静态资源映射
*/
@Component
public class MyWebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
}
}

输入http://localhost:8080/static/java.png就可以访问了

Buy me a coffee
静听、唯美殇 微信 微信
静听、唯美殇 支付宝 支付宝