SpringBoot静态资源⽂件位置
SpringBoot可以JAR/WAR的形式启动运⾏,有时候静态资源的访问是必不可少的,⽐如:image、js、css 等资源的访问。
实⽤性不⼤,简单了解即可。
public class WebMvcAutoConfiguration {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!sourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
Duration cachePeriod = Cache().getPeriod();
CacheControl cacheControl = Cache().getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(
}
String staticPathPattern = StaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(ge }
}
}
}
从代码中可以看出:所有/webjars/**,都去classpath:/META-INF/resources/webjars/资源。
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>
启动服务,测试访问静态地址:
127.0.0.1:9091/hp/webjars/jquery/3.4.1/jquery.js(hp是应⽤上下⽂, t-path = /hp)
@ConfigurationProperties(prefix = "sources", ignoreUnknownFields = false)
public class ResourceProperties {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" };
/**
* Locations of static resources. Defaults to classpath:[/META-INF/resources/,
* /resources/, /static/, /public/].
*/
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
/**
* Whether to enable default resource handling.
*/
private boolean addMappings = true;
private final Chain chain = new Chain();
private final Cache cache = new Cache();
public String[] getStaticLocations() {
return this.staticLocations;
}
public void setStaticLocations(String[] staticLocations) {
this.staticLocations = appendSlashIfNecessary(staticLocations);
}
private String[] appendSlashIfNecessary(String[] staticLocations) {
String[] normalized = new String[staticLocations.length];
for (int i = 0; i < staticLocations.length; i++) {
String location = staticLocations[i];
normalized[i] = dsWith("/") ? location : location + "/";
}
return normalized;
}
public boolean isAddMappings() {
return this.addMappings;
}
public void setAddMappings(boolean addMappings) {
this.addMappings = addMappings;
}
public Chain getChain() {
return this.chain;
}
public Cache getCache() {
return this.cache;
}
.
.....
spring怎么读取jar文件......
}
View Code
SpringBoot提供了⼏种默认的资源路径:
classpath:/META-INF/resources/ > classpath:/resources/ > classpath:/static/ > classpath:/public/
备注说明: "/"=>当前项⽬的根路径
我们在src/main/resources⽬录下新建 public、resources、static 、META-INF等⽬录⽬录,并分别放⼊ 1.jpg 2.jpg 3.jpg 4.jpg 5.jpg 五张图⽚。
访问图⽚地址:127.0.0.1:9091/hp/1.jpg,只有5.jpg访问不到。
springboot访问静态资源,默认有两个默认⽬录:
⼀个是 src/mian/resource⽬录(上⾯将的就是这种情况)
⼀个是 ServletContext 根⽬录下( src/main/webapp )
⼀般来说 src/main/java ⾥⾯放Java代码,resource ⾥⾯放配置⽂件、xml, webapp⾥⾯放页⾯、js之类的。
⼀般创建的maven项⽬⾥⾯都没有 webapp ⽂件夹,在这⾥我们⾃⼰在 src/main ⽬录下创建⼀个 webapp项⽬⽬录。
我们在sources.static-locations后⾯追加⼀个配置classpath:/os/:
# 静态⽂件请求匹配⽅式
spring.mvc.static-path-pattern=/**
# 修改默认的静态寻址资源⽬录多个使⽤逗号分隔
源码:
public class WebMvcAutoConfiguration {
private Optional<Resource> getWelcomePage() {
String[] locations = StaticLocations());
return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
}
private Resource getIndexHtml(String location) {
Resource(location + "index.html");
}
}
1. 直接设置静态默认页⾯
静态资源⽂件夹下的所有index.html页⾯,被"/**"映射;
2. 增加控制器的⽅式
(1) 新增模版引擎的⽀持
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
(2) 配置核⼼⽂件application.properties
server.port=9091
t-path=/hp
spring.mvc.view.prefix=classpath:/templates/
没有设置后缀名。
(3) 设置路由
@Controller
public class IndexController {
@GetMapping({"/","/index"})
public String index(){
return "default";
}
}
访问127.0.0.1:9091/hp/。
3. 设置默认的View跳转页⾯
(1) 新增模版引擎的⽀持
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
(2) 配置核⼼⽂件application.properties
server.port=9091
t-path=/hp
spring.mvc.view.prefix=classpath:/templates/
(3) 启动⽂件的修改如下
@SpringBootApplication
public class Demo05BootApplication implements WebMvcConfigurer { @Override
public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("default");
registry.addViewController("/index1").setViewName("default");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
public static void main(String[] args) {
SpringApplication.run(Demo05BootApplication.class, args);
}
}
4. Favicon设置
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论