SpringBoot Web开发
- 要解决的问题
- 静态资源导入问题
- 首页
- jsp,模板引擎
- 增删改查
- 拦截器
- 国际化
SpringBoot 静态资源问题
1 SpringBoot 如何处理静态资源问题 ,那些目录下的静态能够被访问
//静态资源处理方法
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//是否在yaml 自定义了映射关系
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
//缓存的处理
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
// 如果有访问/webjars 则映射到classpath:/META-INF/resources/webjars/
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
// staticPathPattern=“/**” 访问/** 则映射到{ "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/" };上面的位置
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
小结
SpringBoot 访问静态资源的处理方式有三种
1 在yaml自定义静态资源映射处理
2 使用webjars /webjars/**
3 使用默认的静态资源映射处理
问/** 则映射到{ "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/" };
测试
1 yaml 中自定静态资源映射处理
- 1 创建一个SpringBoot项目
- 2 修改配置文件 application.yaml
spring:
resources:
static-locations: ["classpath:/hello/"]
3 resource文件下添加文件 hello.html
4 访问http://localhost:8080/hello.html
访问http://localhost:8080/hi.html 结果
自定义静态资源映射路径后,默认的静态资源路径就会失效
2 默认静态资源映射路径
1 去掉上面的配置文件,配置的静态资源路径
2 访问访问http://localhost:8080/hi.html 结果
3 webjars
webjars 一种个webjars格式的web库
1官网https://www.webjars.org/
2 导入依赖
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.5.1</version>
</dependency>
3 找到webjars
4访问里面的jquery.js文件
http://localhost:8080/webjars/jquery/3.5.1/jquery.js
结果