当前位置: 首页 > news >正文

网站建设 销售提成阿里云万网域名查询

网站建设 销售提成,阿里云万网域名查询,盐城市城乡建设局网站,互联网策划主要做什么文章目录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、webjar
  • 2、欢迎页支持
  • 3、自定义 Favicon
  • 4、静态资源配置原理
    • 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/** 下的资源

官网

<dependency><groupId>org.webjars</groupId><artifactId>jquery</artifactId><version>3.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的beanFactory(spring的容器)
HttpMessageConverters找到所有的HttpMessageConverters
ResourceHandlerRegistrationCustomizer找到 资源处理器的自定义器。重点
DispatcherServletPathDispatcherServlet处理的路径
ServletRegistrationBean给应用注册Servlet、Filter…
public WebMvcAutoConfigurationAdapter(ResourceProperties resourceProperties, WebMvcProperties mvcProperties,ListableBeanFactory beanFactory, ObjectProvider<HttpMessageConverters> messageConvertersProvider,ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider,ObjectProvider<DispatcherServletPath> dispatcherServletPath,ObjectProvider<ServletRegistrationBean<?>> 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-mappings=true(false禁用静态资源路径映射)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, Optional<Resource> 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功能,当添加访问静态资源前缀,就会找不到。

http://www.wooajung.com/news/27749.html

相关文章:

  • 东莞网站优化方案海南百度推广公司有哪些
  • 深圳 网站设计营销推广公司
  • 网站制作 需要什么网络技术百度搜索风云榜排行榜
  • 网站建站行业公司主页建设天津百度推广网络科技公司
  • 做纸巾定制的网站新闻早知道
  • 网站建设技术手段南京百度网站快速优化
  • 网站建设与管理课程心得体会中文网站排名
  • flash做网站通栏每日新闻快报
  • 昆明网站做定制网站开发
  • 拍卖网站制作微信公众号营销
  • 网站扫描怎么做百度搜索引擎的网址是多少
  • 沈阳商城网站建设seo教学网站
  • 源码网站大淘客cms外包
  • 新开传奇网站首区长沙推广公司
  • 福步外贸论坛app上海有实力的seo推广咨询
  • 男女做姿抽插视频网站sem是什么设备
  • 网站登录页面模板成功营销十大经典案例
  • 商城开发网站建设小程序生成平台系统
  • 英语网站都可以做哪些内容百度推广需要多少钱
  • 营销网站推荐网络搭建教程
  • vs如何做网站营销技巧和话术
  • 做会展网站的公司的工作流程深圳网
  • 响应式网站做客服弹窗app推广员怎么做
  • 企业注册登记宁波seo外包推广排名
  • 网站建设开发程序推广平台免费b2b网站大全
  • 自建b2b代表网站网站建设哪个公司好
  • 如何在网站上做404页面seo课程简介
  • 做地方房产网站怎么样中国搜索引擎大全
  • 郑州新密网站建设中文搜索引擎网站
  • 宁波建设网站哪家好郑州seo技术服务顾问