springboot静态资源映射规则,springboot 静态资源配置
00-1010 1.相关概念2。静态资源目录3。静态资源访问前缀4。欢迎页面支持5。自定义图标6。源代码分析
目录
Spring Boot默认为我们提供了静态资源处理,使用WebMvcAutoConfiguration中各种属性的配置。建议使用Spring Boot的默认配置方式,如果需要特殊处理,可以通过配置文件进行修改。如果想完全自己控制WebMVC,需要将@EnableWebMvc添加到@Configuration批注的配置类中。添加这个注释后,WebMvcAutoConfiguration中的配置将不会生效,需要配置所有需要的东西(可以使用继承)。
00-1010默认情况下,只要静态资源放在类路径下(resources):
/静态
/公共
/资源
/META-INF/资源
浏览器访问:当前项目根路径/静态资源名称
请进来,先去控制器看看你能不能处理。所有不能被处理的请求都被移交给静态资源处理器。如果找不到静态资源,请响应第404页。
让我们在控制器中编写一个测试方法来测试它。
在控制器中注释了方法之后
您还可以更改默认的静态资源路径,并且/static、/public、/resources、/meta-INF/resources将变为无效。
应用程序.属性
#静态资源路径spring . resources . static-locations=class path :/dir 1/,classpath3360/dir2/
1.相关概念
应用程序.属性
#静态资源访问前缀,即前缀为spring . MVC . Static-path-pattern=/RES/* *的浏览器URL路径
00-1010表示当URL上没有访问地图时,会自动跳转到欢迎页面,
静态资源路径下的Index.html。
您可以配置静态资源的路径,但不能配置静态资源的访问前缀。否则,默认情况下无法访问index.html。
00-1010指网页标签上的小图标。
Favicon.ico可以放在静态资源目录中。
2.静态资源目录
SpringBoot启动自动配置类WebMvcAutoConfiguration加载xxxAutoConfiguration类(自动配置类)SpringMVC函数默认生效@ auto configuration(after={ dispatcherservetautoconfiguration . class,taskexecutionautoconfiguration . class,validation auto configuration . class })@ ConditionalOnWebApplication(Type=Type。秒
ERVLET)@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})@AutoConfigureOrder(-2147483638)public class WebMvcAutoConfiguration { public static final String DEFAULT_PREFIX = ""; public static final String DEFAULT_SUFFIX = ""; public static final PathPatternParser pathPatternParser = new PathPatternParser(); private static final String SERVLET_LOCATION = "/"; public WebMvcAutoConfiguration() { }给容器中配置的内容:
配置文件的相关属性的绑定:WebMvcProperties == spring.mvc、WebProperties==spring.web
@Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class}) @EnableConfigurationProperties({WebMvcProperties.class, WebProperties.class}) @Order(0) public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ServletContextAware { private static final Log logger = LogFactory.getLog(WebMvcConfigurer.class); private final Resources resourceProperties; private final WebMvcProperties mvcProperties; private final ListableBeanFactory beanFactory; private final ObjectProvider<HttpMessageConverters> messageConvertersProvider; private final ObjectProvider<DispatcherServletPath> dispatcherServletPath; private final ObjectProvider<ServletRegistrationBean<?>> servletRegistrations; private final WebMvcAutoConfiguration.ResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizer; private ServletContext servletContext;
配置类只有一个有参构造器
public WebMvcAutoConfigurationAdapter(WebProperties webProperties, WebMvcProperties mvcProperties, ListableBeanFactory beanFactory, ObjectProvider<HttpMessageConverters> messageConvertersProvider, ObjectProvider<WebMvcAutoConfiguration.ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider, ObjectProvider<DispatcherServletPath> dispatcherServletPath, ObjectProvider<ServletRegistrationBean<?>> servletRegistrations) { this.resourceProperties = webProperties.getResources(); this.mvcProperties = mvcProperties; this.beanFactory = beanFactory; this.messageConvertersProvider = messageConvertersProvider; this.resourceHandlerRegistrationCustomizer = (WebMvcAutoConfiguration.ResourceHandlerRegistrationCustomizer)resourceHandlerRegistrationCustomizerProvider.getIfAvailable(); this.dispatcherServletPath = dispatcherServletPath; this.servletRegistrations = servletRegistrations; this.mvcProperties.checkConfiguration(); }
ResourceProperties resourceProperties;获取和spring.resources绑定的所有的值的对象WebMvcProperties mvcProperties 获取和spring.mvc绑定的所有的值的对象ListableBeanFactory beanFactory Spring的beanFactoryHttpMessageConverters 找到所有的HttpMessageConvertersResourceHandlerRegistrationCustomizer 找到 资源处理器的自定义器。DispatcherServletPathServletRegistrationBean 给应用注册Servlet、Filter…资源处理的默认规则
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}); } }); } }
根据上述代码,我们可以同过配置禁止所有静态资源规则。
application.properties
#禁用所有静态资源规则spring.web.resources.add-mappings=false
静态资源处理规则:
public static class Resources { private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"}; private String[] staticLocations; private boolean addMappings; private boolean customized; private final WebProperties.Resources.Chain chain; private final WebProperties.Resources.Cache cache;
欢迎页处理规则:
@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; }
WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders, ApplicationContext applicationContext, Resource welcomePage, String staticPathPattern) { if (welcomePage != null && "/**".equals(staticPathPattern)) { logger.info("Adding welcome page: " + welcomePage); this.setRootViewName("forward:index.html"); } else if (this.welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) { logger.info("Adding welcome page template: index"); this.setRootViewName("index"); } }
到此这篇关于SpringBoot Web详解静态资源规则与定制化处理的文章就介绍到这了,更多相关SpringBoot静态资源规则与定制内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。