100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > html资源文件放在哪里 09 Spring Boot开发web项目之静态资源放哪里?

html资源文件放在哪里 09 Spring Boot开发web项目之静态资源放哪里?

时间:2023-01-04 13:13:46

相关推荐

html资源文件放在哪里 09 Spring Boot开发web项目之静态资源放哪里?

Spring Boot开发web项目之静态资源放哪里?

先了解自动装配autoconfiguration

这些内容是spring boot天然集成好的框架

找到WebMvcAutoConfigration.java

方式1:webjars

【1】一个神奇的网站

【2】复制jquery任意版本的maven坐标粘贴到web项目中

【3】在哪儿看这个jquery是否导入成功

【4】启动项目之后,如何通过http请求访问这个jquery.js呢?

方式2:/**

【1】静态资源存放路径

【2】说明四个路径的位置

【3】请求路径

【4】新建四个不同的html静态文件放在四个目录下,测试访问

配置web项目的首页

【1】找到请求路径和页面存放位置

【2】首页的名字叫什么,后缀叫什么?

【3】结论,首页只要取名叫index.html放到上面4个目录任意一个即可

为了保证安全可以修改默认的目录

properties文件修改配置

先了解自动装配autoconfiguration

这些内容是spring boot天然集成好的框架

找到WebMvcAutoConfigration.java

方式1:webjars

webjars:前端的静态资源可以是jar包的方式

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

if (!this.resourceProperties.isAddMappings()) {

logger.debug("Default resource handling disabled");

return;

}

Duration cachePeriod = this.resourceProperties.getCache().getPeriod();

CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();

if (!registry.hasMappingForPattern("/webjars/**")) {

customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")

.addResourceLocations("classpath:/META-INF/resources/webjars/")

.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));

}

String staticPathPattern = this.mvcProperties.getStaticPathPattern();

if (!registry.hasMappingForPattern(staticPathPattern)) {

customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)

.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))

.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));

}

}

【1】一个神奇的网站

传送到

【2】复制jquery任意版本的maven坐标粘贴到web项目中

【3】在哪儿看这个jquery是否导入成功

点开jar包就能看见

【4】启动项目之后,如何通过http请求访问这个jquery.js呢?

这里的pathPattern:表示访问路径叫/webjars/

addResourceLocations(“classpath:/META-INF/resources/webjars/”)表示资源路径,jquery.js好像就在这个路径下

http://localhost:8080/webjars/jquery/1.12.1/jquery.js,访问成功

方式2:/**

【1】静态资源存放路径

classpath:/META-INF/resources/

classpath:/resources/

classpath:/static/

classpath:/public/

这里有4个路径

【2】说明四个路径的位置

【3】请求路径

语法:http://localhost:8080/html文件的名字即可

http://localhost:8080/meta_inf_resources.html

http://localhost:8080/public.html

http://localhost:8080/resources.html

http://localhost:8080/static.html

【4】新建四个不同的html静态文件放在四个目录下,测试访问

访问/META-INF/resources目录静态资源

访问/public目录静态资源

访问/resources目录静态资源

访问/static/目录静态资源

配置web项目的首页

【1】找到请求路径和页面存放位置

请求路径还是:/**

页面存放位置还是上面的4个位置

classpath:/META-INF/resources/

classpath:/resources/

classpath:/static/

classpath:/public/

【2】首页的名字叫什么,后缀叫什么?

【3】结论,首页只要取名叫index.html放到上面4个目录任意一个即可

访问首页的方式,不需要指定文件名字

http://localhost:8080/

为了保证安全可以修改默认的目录

在application.properties文件修改

properties文件修改配置

# 这种方式会覆盖系统的四个目录,之后系统就不认识默认的4个目录了

spring.resources.static-locations=classpath:/test/,classpath:/test2

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。