建设微信商城网站保定网站开发
- 作者: 五速梦信息网
- 时间: 2026年04月20日 10:45
当前位置: 首页 > news >正文
建设微信商城网站,保定网站开发,站长工具2023最新国产,做视频网站利润如何处理文章目录1、静态资源访问1.1、静态资源目录1.2、如果静态资源与controller资源重名1.3、改变默认的静态资源路径1.4、修改静态资源访问前缀1.5、webjar2、欢迎页支持3、自定义 Favicon4、静态资源配置原理4.1、与Web开发有关的相关自动配置类4.2、WebMvcAutoConfiguration 注解…
文章目录1、静态资源访问1.1、静态资源目录1.2、如果静态资源与controller资源重名1.3、改变默认的静态资源路径1.4、修改静态资源访问前缀1.5、webjar2、欢迎页支持3、自定义 Favicon4、静态资源配置原理4.1、与Web开发有关的相关自动配置类4.2、WebMvcAutoConfiguration 注解介绍4.3、WebMvcAutoConfiguration 功能介绍4.4 WebMvcAutoConfigurationAdapter 内部类1、唯一有参构造器2、资源处理的默认规则4.5 EnableWebMvcConfiguration 内部类1、欢迎页的处理规则4.6、favicon【尚硅谷】SpringBoot2零基础入门教程-讲师雷丰阳 笔记 路还在继续梦还在期许 1、静态资源访问
1.1、静态资源目录
默认静态资源放在类路径下 resources 下 /static 或 /public 或 /resources 或 /META-INF/resources 目录下
访问地址 当前项目根路径/ 静态资源名
原理 写静态资源名自动找到静态资源静态资源映射/。
spring:# 默认mvc:static-path-pattern: //的意思是所有文件夹及里面的子文件夹 /*是所有文件夹不含子文件夹
1.2、如果静态资源与controller资源重名
请求进来先去找Controller看能不能处理。
不能处理的所有请求又都交给静态资源处理器。
静态资源也找不到则响应404页面
1.3、改变默认的静态资源路径
spring:# 默认mvc:static-path-pattern: /# 改变默认的静态资源路径resources:static-locations: [classpath:/haha/]1.4、修改静态资源访问前缀
添加前缀
spring:# 添加前缀mvc:static-path-pattern: /res/访问地址当前项目 static-path-pattern的值 静态资源名 静态资源文件夹下找
1.5、webjar
可以自动映射 /webjars/ 下的资源
官网
dependencygroupIdorg.webjars/groupIdartifactIdjquery/artifactIdversion3.5.1/version
/dependency存放路径META-INF/resources/webjars/jquery/3.5.1/jquery.js 访问地址http://localhost:8080/webjars/jquery/3.5.1/jquery.js 后面地址要按照依赖里面的包路径
2、欢迎页支持
静态资源路径下 放 index.html 可以配置静态资源路径但是不可以配置静态资源的访问前缀。否则导致 index.html不能被默认访问
spring:
mvc:
static-path-pattern: /res/** 这个会导致welcome page功能失效resources:static-locations: [classpath:/haha/]controller能处理/index
3、自定义 Favicon favicon.ico 放在静态资源目录下spring boot 会自动将这个名字的图标当成应用的图标。 spring:
mvc:
static-path-pattern: /res/** 这个会导致 Favicon 功能失效4、静态资源配置原理
spring boot 启动时会默认加载 xxxAutoConfiguration 类自动配置类这里从配置类入手查看源码。 4.1、与Web开发有关的相关自动配置类 DispatcherServletAutoConfiguration: 配置SpringNVC中DispatcherServlet HttpEncodingAutoConfiguration 配置编解码 MultipartAutoConfiguration 配置文件上传 ServletWebServerFactoryAutoConfiguration 配置服务器 WebMvcAutoConfiguration SpringMVC功能的自动配置类 4.2、WebMvcAutoConfiguration 注解介绍 // 当前类为配置类组件之间无依赖关系 Configuration(proxyBeanMethods false) // 程序是Servlet应用 ConditionalOnWebApplication(type Type.SERVLET) // 导入Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class 这三个类 ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class }) // 容器中没有 WebMvcConfigurationSupport.class 组件当前类中配置才生效 ConditionalOnMissingBean(WebMvcConfigurationSupport.class) AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE 10) AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,ValidationAutoConfiguration.class }) public class WebMvcAutoConfiguration {}4.3、WebMvcAutoConfiguration 功能介绍 位置org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration // SpringMVC用于兼容REST风格 Bean ConditionalOnMissingBean(HiddenHttpMethodFilter.class) ConditionalOnProperty(prefix spring.mvc.hiddenmethod.filter, name enabled, matchIfMissing false) public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {return new OrderedHiddenHttpMethodFilter(); }// 表单内容过滤器 Bean ConditionalOnMissingBean(FormContentFilter.class) ConditionalOnProperty(prefix spring.mvc.formcontent.filter, name enabled, matchIfMissing true) public OrderedFormContentFilter formContentFilter() {return new OrderedFormContentFilter(); }4.4 WebMvcAutoConfigurationAdapter 内部类 位置org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter // Defined as a nested config to ensure WebMvcConfigurer is not read when not // on the classpath Configuration(proxyBeanMethods false) Import(EnableWebMvcConfiguration.class) // 把配置文件的相关属性与WebMvcProperties(spring.mvc)、ResourceProperties进行绑定(spring.resources) EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class }) Order(0) public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {}1、唯一有参构造器 有参构造器所有参数的值都会从容器中确定 形参作用ResourceProperties resourceProperties获取和spring.resources绑定的所有的值的对象WebMvcProperties mvcProperties获取和spring.mvc绑定的所有的值的对象ListableBeanFactory beanFactorySpring的beanFactoryspring的容器HttpMessageConverters找到所有的HttpMessageConvertersResourceHandlerRegistrationCustomizer找到 资源处理器的自定义器。重点DispatcherServletPathDispatcherServlet处理的路径ServletRegistrationBean给应用注册Servlet、Filter… public WebMvcAutoConfigurationAdapter(ResourceProperties resourceProperties, WebMvcProperties mvcProperties,ListableBeanFactory beanFactory, ObjectProviderHttpMessageConverters messageConvertersProvider,ObjectProviderResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizerProvider,ObjectProviderDispatcherServletPath dispatcherServletPath,ObjectProviderServletRegistrationBean? servletRegistrations) {this.resourceProperties resourceProperties;this.mvcProperties mvcProperties;this.beanFactory beanFactory;this.messageConvertersProvider messageConvertersProvider;this.resourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizerProvider.getIfAvailable();this.dispatcherServletPath dispatcherServletPath;this.servletRegistrations servletRegistrations; }2、资源处理的默认规则 添加资源处理器 Override public void addResourceHandlers(ResourceHandlerRegistry registry) {// 拿到与配置文件绑定的属性spring.resources.add-mappingstruefalse禁用静态资源路径映射if (!this.resourceProperties.isAddMappings()) {//true不进入false进入logger.debug(Default resource handling disabled);return;}// 获取到配置文件中静态资源的缓存策略所有的静态资源在浏览器默认存多少秒Duration cachePeriod this.resourceProperties.getCache().getPeriod();// 注册第一种访问规则/webjars/CacheControl cacheControl this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();// 在项目中寻找静态资源if (!registry.hasMappingForPattern(/webjars/)) {customizeResourceHandlerRegistration(registry.addResourceHandler(/webjars/).addResourceLocations(classpath:/META-INF/resources/webjars/).setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));}// 获取配置文件中spring.mvc.static-path-pattern的值没有配置就使用默认值/String staticPathPattern this.mvcProperties.getStaticPathPattern();// 在项目中寻找默认位置{ classpath:/META-INF/resources/,classpath:/resources/, classpath:/static/, classpath:/public/ };// 默认的位置也可以修改if (!registry.hasMappingForPattern(staticPathPattern)) {customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));} }spring:resources:add-mappings: false #禁用所有静态资源规则cache:period: 1100 # 以秒为单位4.5 EnableWebMvcConfiguration 内部类 /*** Configuration equivalent to {code EnableWebMvc}.*/ Configuration(proxyBeanMethods false) public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {} 1、欢迎页的处理规则 HandlerMapping处理器映射保存了每一个Handler能处理哪些请求。 利用反射向容器中注入WelcomePageHandlerMapping 作用寻找谁可以处理欢迎页的映射。 Bean public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {WelcomePageHandlerMapping welcomePageHandlerMapping new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),this.mvcProperties.getStaticPathPattern()); // 获取spring.mvc.static-path-pattern 的值welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());return welcomePageHandlerMapping; }位置org.springframework.boot.autoconfigure.web.servlet.WelcomePageHandlerMapping final class WelcomePageHandlerMapping extends AbstractUrlHandlerMapping {WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders,ApplicationContext applicationContext, OptionalResource welcomePage, String staticPathPattern) {// 条件欢迎页存在并且路径是默认/if (welcomePage.isPresent() /.equals(staticPathPattern)) {logger.info(Adding welcome page: welcomePage.get());setRootViewName(forward:index.html);}else if (welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) {// 调用Controller看谁能处理/indexlogger.info(Adding welcome page template: index);setRootViewName(index);}} }4.6、favicon 与代码无关浏览器默认发送当前项目下的favicon功能当添加访问静态资源前缀就会找不到。
- 上一篇: 建设微网站wordpress %s
- 下一篇: 建设微信网站要多少钱鹰潭做网站
相关文章
-
建设微网站wordpress %s
建设微网站wordpress %s
- 技术栈
- 2026年04月20日
-
建设网站转赚钱吗厦门网站建设 金猪
建设网站转赚钱吗厦门网站建设 金猪
- 技术栈
- 2026年04月20日
-
建设网站注意哪几点分类信息的网站如何推广
建设网站注意哪几点分类信息的网站如何推广
- 技术栈
- 2026年04月20日
-
建设微信网站要多少钱鹰潭做网站
建设微信网站要多少钱鹰潭做网站
- 技术栈
- 2026年04月20日
-
建设西安网站个人域名做邮箱网站
建设西安网站个人域名做邮箱网站
- 技术栈
- 2026年04月20日
-
建设项目查询网站精简wordpress博客程序
建设项目查询网站精简wordpress博客程序
- 技术栈
- 2026年04月20日
