文章总结:作为一个后端开发,在Springboot中怎样引入需要的js依赖以及常用的静态资源映射呢?SpringBoot已经给做好了自动化配置,使用时只需要按照默认的配置去放相应的文件,就可以快速上手。
1、创建SpringBoot web项目(参考之前文章-使用Spring Initializer快速创建Spring Boot项目)
1)创建SpringBoot 应用,选择需要的模块;
2)SpringBoot 已经默认配置好相应的场景,只需指定少量配置就可以运行;
3)编写业务代码
2、SpringBoot web的自动配置会加载那些配置
xxxxAutoConfiguration:给容器中自动配置组件;xxxxProperties:配置类来封装配置文件的内容;
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnWebApplication(
type = Type.SERVLET
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {
//Web 加载的配置组件
@Deprecated
@ConfigurationProperties(
prefix = "spring.resources",
ignoreUnknownFields = false
)
public class ResourceProperties extends Resources {
// 配置文件可以设置和静态资源有关的参数,缓存时间等
3、SpringBoot对静态资源的映射规则;
1)添加静态资源文件映射 :WebMvcAutoConfiguration 中的addResourceHandlers 方法
public void addResourceHandlers(ResourceHandlerRegistry registry) {
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});
}
});
}
}
怎么通过maven配置添加Jquery的依赖?所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源
- 添加maven依赖(去 https://www.webjars.org/ 官网找到相应的JQuery 相应的配置)
<!--引入jquery-webjar-->在访问的时只需要写webjars下面资源的名称即可
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.5.1</version>
</dependency>
-
重启项目访问lIP:端口/webjars/jquery/3.3.1/jquery.js 即可
-
“/**” 访问当前项目的任何资源,都去(静态资源的文件夹)找相应映射默认寻找路径如下
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":当前项目的根路径
IP:端口/abc === 去静态资源文件夹里面找abc
2)配置欢迎页:WebMvcAutoConfiguration 中的addResourceHandlers 方法
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider));
welcomePageHandlerMapping.setCorsConfigurations(this.getCorsConfigurations());
return welcomePageHandlerMapping;
private Resource getWelcomePage() {
String[] var1 = this.resourceProperties.getStaticLocations();
int var2 = var1.length;
for(int var3 = 0; var3 < var2; ++var3) {
String location = var1[var3];
Resource indexHtml = this.getIndexHtml(location);
if (indexHtml != null) {
return indexHtml;
}
}
ServletContext servletContext = this.getServletContext();
if (servletContext != null) {
return this.getIndexHtml((Resource)(new ServletContextResource(servletContext, "/")));
} else {
return null;
}
}
private Resource getIndexHtml(String location) {
return this.getIndexHtml(this.resourceLoader.getResource(location));
}
private Resource getIndexHtml(Resource location) {
try {
Resource resource = location.createRelative("index.html");
if (resource.exists() && resource.getURL() != null) {
return resource;
}
} catch (Exception var3) {
;
}
return null;
}
具体的是那个配置页面,可以根据代码可以找到是静态资源文件夹下的所有index.html页面,
访问IP:port/ 就会去静态文件夹下找index.html
- 配置角标:所有的 **/favicon.ico 都是在静态资源文件下找(下面的源码Springboot 版本为1.5.3)
//配置喜欢的图标
@Configuration
@ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)
public static class FaviconConfiguration {
private final ResourceProperties resourceProperties;
public FaviconConfiguration(ResourceProperties resourceProperties) {
this.resourceProperties = resourceProperties;
}
@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
//所有 **/favicon.ico
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
faviconRequestHandler()));
return mapping;
}
@Bean
public ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
requestHandler
.setLocations(this.resourceProperties.getFaviconLocations());
return requestHandler;
}
}
总结:之前只会从静态文件夹里引入常用的全框架js,通过此篇文章知道了,还可以通过maven引入;想要配置欢迎页和角标都是在默认的静态文件下的index.heml和favicon.ico 。