Spring-Web开发初始静态资源处理《课时九》了解什么是Webjars和静态资源所放在的位置为什么是这样放的深入源码了解其核心重点在源码上哦!
1 在新建的项目中 Ctrl+N搜索下面的类
要了解的目标信息
package org.springframework.boot.autoconfigure.web.servlet; //部分代码省略 @AutoConfiguration( after = {DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class} ) @ConditionalOnWebApplication( type = Type.SERVLET ) @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class}) @ConditionalOnMissingBean({WebMvcConfigurationSupport.class}) @AutoConfigureOrder(-2147483638) public class WebMvcAutoConfiguration {
2 在进入上面的类后ctrl+r搜索 addResourceHandlers方法
如图所示了解资源增加在哪里
//551 //将源码赋值过来 public void addResourceHandlers(ResourceHandlerRegistry registry) { //1什么是webjars if (!this.resourceProperties.isAddMappings()) { logger.debug("Default resource handling disabled"); } else { this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/"); this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> { registration.addResourceLocations(this.resourceProperties.getStaticLocations()); if (this.servletContext != null) { ServletContextResource resource = new ServletContextResource(this.servletContext, "/"); registration.addResourceLocations(new Resource[]{resource}); } }); } }
什么是Webjars 在百度上搜索
打开官网 WebJars - Web Libraries in Jars
Jquery的依赖
复制依赖引入到 Pom.xml文件中
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 引用jquery的文件依赖--> <dependency> <groupId>org.webjars.npm</groupId> <artifactId>jquery</artifactId> <version>3.6.1</version> </dependency> </dependencies>
然后观察发现多了一个架包
如图所示
结论一:静态资源存放在在:
addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
静态资源还有第二种方式
this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> { registration.addResourceLocations(this.resourceProperties.getStaticLocations()); if (this.servletContext != null) { ServletContextResource resource = new ServletContextResource(this.servletContext, "/"); registration.addResourceLocations(new Resource[]{resource}); }
this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {}
点击上面红色标记跳转如图所示
静态资源的第二种方式
结论二:/**也可以存放静态资源 找到下面的代码信息往下翻
@ConfigurationProperties("spring.web") public class WebProperties { private Locale locale; private WebProperties.LocaleResolver localeResolver; private final WebProperties.Resources resources; public WebProperties() { this.localeResolver = WebProperties.LocaleResolver.ACCEPT_HEADER; this.resources = new WebProperties.Resources(); }
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
如图所示的几个包可以存放静态资源
总结:今天说的是静态资源存放位置是依据SpringBoot源码中得来的.要想知道更深的角度必要是要读SpringBoot源码
1.在springboot,我们可以使用以下方式处理静态资源。
webjars 1ocalhost: 8080/webjars/
public, static, /**, resources
localhost : 8080/
2.优先级: resources>static>public
Spring.Boot Web 模板引擎和首页为什么默认的是index.html页面呀《课时十》_星辰镜的博客-CSDN博客