欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot对静态资源的映射规则

程序员文章站 2022-07-10 19:57:05
...

第一种映射规则:

WebMvcAutoConfiguration类中addResourceHandlers(添加资源映射)方法的部分源码:

		if (!registry.hasMappingForPattern("/webjars/**")) {
				customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
						.addResourceLocations("classpath:/META-INF/resources/webjars/")
						.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
			}

解释:所有/webjars/**,都去classpath:/META-INF/resources/webjars/找资源;
webjars :以jar包的方式引入静态资源;
webjars官网链接:https://www.webjars.org/

springboot对静态资源的映射规则

在pom文件中引用webjars

springboot对静态资源的映射规则

导入包的目录结构:

springboot对静态资源的映射规则

在访问的时候只需要写webjars下面资源的名称即可:http://localhost:8080/webjars/jquery/3.4.1/jquery.js

springboot对静态资源的映射规则

这个类可以设置和静态资源有关的参数,缓存时间等

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {

springboot对静态资源的映射规则

第二种映射规则:

“/**” 访问当前项目的任何资源(静态资源的文件夹),默认从这些文件夹寻找;

"classpath:/META-INF/resources/",
"classpath:/resources/", 
"classpath:/static/", 
"classpath:/public/",
"/";//当前项目的根路径

列如:访问static→jquery→jquery-3.4.1.slim.js

springboot对静态资源的映射规则

访问:http://localhost:8080/jquery/jquery-3.4.1.slim.js;如图:

springboot对静态资源的映射规则

配置欢迎页面的映射

WebMvcAutoConfiguration类的welcomePageHandlerMapping(配置欢迎页面)方法:

        @Bean
		public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
			WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
					new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
					this.mvcProperties.getStaticPathPattern());
			welcomePageHandlerMapping.setInterceptors(getInterceptors());
			return welcomePageHandlerMapping;
		}

欢迎页;静态资源文件夹下的所有index.htm页面;被"/**"映射;

springboot对静态资源的映射规则

列如:http://localhost:8080/,访问index界面;

springboot对静态资源的映射规则

设置图标映射

WebMvcAutoConfiguration类的FaviconConfiguration(设置图标映射)方法:

		@Configuration
		@ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)
		public static class FaviconConfiguration implements ResourceLoaderAware {

			private final ResourceProperties resourceProperties;

			private ResourceLoader resourceLoader;

			public FaviconConfiguration(ResourceProperties resourceProperties) {
				this.resourceProperties = resourceProperties;
			}

			@Override
			public void setResourceLoader(ResourceLoader resourceLoader) {
				this.resourceLoader = resourceLoader;
			}

			@Bean
			public SimpleUrlHandlerMapping faviconHandlerMapping() {
				SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
				mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
				mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", faviconRequestHandler()));
				return mapping;
			}

			@Bean
			public ResourceHttpRequestHandler faviconRequestHandler() {
				ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
				requestHandler.setLocations(resolveFaviconLocations());
				return requestHandler;
			}

			private List<Resource> resolveFaviconLocations() {
				String[] staticLocations = getResourceLocations(this.resourceProperties.getStaticLocations());
				List<Resource> locations = new ArrayList<>(staticLocations.length + 1);
				Arrays.stream(staticLocations).map(this.resourceLoader::getResource).forEach(locations::add);
				locations.add(new ClassPathResource("/"));
				return Collections.unmodifiableList(locations);
			}

		}

springboot对静态资源的映射规则

所有的**/favicon.ico都是在静态资源文件下寻找;

springboot对静态资源的映射规则

如图:

springboot对静态资源的映射规则

通过配置文件改变静态资源默认访问路径

ResourceProperties类部分源码:

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
			"classpath:/resources/", "classpath:/static/", "classpath:/public/" };

更改相关配置:

spring.resources.static-locations=classpath:/holle,classpath:/godh

springboot对静态资源的映射规则

放在默认的访问静态资源文件夹中index.html,text.html放在自己配置的访问静态资源的文件夹。如图:

springboot对静态资源的映射规则

结果,如图:

springboot对静态资源的映射规则

相关标签: springboot