您好,登錄后才能下訂單哦!
今天小編給大家分享一下SpringBoot Web靜態(tài)資源規(guī)則與定制化怎么處理的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
Spring Boot 默認為我們提供了靜態(tài)資源處理,使用 WebMvcAutoConfiguration 中的配置各種屬性。
建議使用Spring Boot的默認配置方式,如果需要特殊處理的再通過配置文件進行修改。
如果想要自己完全控制WebMVC,就需要在@Configuration注解的配置類上增加@EnableWebMvc, 增加該注解以后WebMvcAutoConfiguration中配置就不會生效,你需要自己來配置需要的每一項(可以使用繼承)。
默認只要靜態(tài)資源放在類路徑(resources)下:
/static
/public
/resources
/META-INF/resources
瀏覽器訪問: 當前項目根路徑/ + 靜態(tài)資源名
請求進來,先去找Controller看能不能處理。不能處理的所有請求又都交給靜態(tài)資源處理器。靜態(tài)資源也找不到則響應404頁面。
我們在controller里寫個測試方法來測試一下
把controller里的方法注釋后
也可以改變默認的靜態(tài)資源路徑,/static,/public,/resources, /META-INF/resources失效
application.properties
#靜態(tài)資源路徑
spring.resources.static-locations=classpath:/dir1/,classpath:/dir2/
application.properties
#靜態(tài)資源訪問前綴, 就是瀏覽器網址路徑加前綴
spring.mvc.static-path-pattern=/res/**
就是網址上沒有訪問映射時, 會自動跳轉到歡迎頁,
靜態(tài)資源路徑下 index.html。
可以配置靜態(tài)資源路徑
但是不可以配置靜態(tài)資源的訪問前綴。否則導致 index.html不能被默認訪問
指網頁標簽上的小圖標。
favicon.ico 放在靜態(tài)資源目錄下即可。
SpringBoot啟動默認加載 xxxAutoConfiguration 類(自動配置類)
SpringMVC功能的自動配置類WebMvcAutoConfiguration生效
@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 { 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的beanFactory
HttpMessageConverters 找到所有的HttpMessageConverters
ResourceHandlerRegistrationCustomizer 找到 資源處理器的自定義器。
DispatcherServletPath
ServletRegistrationBean 給應用注冊Servlet、Filter…
資源處理的默認規(guī)則
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}); } }); } }
根據上述代碼,我們可以同過配置禁止所有靜態(tài)資源規(guī)則。
application.properties
#禁用所有靜態(tài)資源規(guī)則
spring.web.resources.add-mappings=false
靜態(tài)資源處理規(guī)則:
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;
歡迎頁處理規(guī)則:
@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靜態(tài)資源規(guī)則與定制化怎么處理”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。