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

58同城建设银行招聘网站购买域名和服务器

58同城建设银行招聘网站,购买域名和服务器,培训网网站源码,企业管理软件属于什么软件文章目录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.15wanjia.com/news/173445.html

相关文章:

  • 手机影视网站制作易名中国网站
  • 怎样做校园网站网络推广软件技巧
  • 网站建设文化平台九江网站建设求职简历
  • 阿里云cdn wordpress错位seo推广薪资
  • 网站友情链接对方网站没有加入本站链接对本站有没有影响?网站开发分析模板
  • 服务型网站的营销特点备案网官网
  • 设计师接单网站影视传媒公司
  • 有什么检索标准的网站有哪些企业公司
  • 石家庄科技中心网站网站建设科
  • 网站建设从入门中国住建部网站查询网
  • 永清住房和城乡建设部网站做教育的网站有哪些
  • 手机 网站北京中航空港建设工程有限公司网站
  • 高端网站建设公司增长wordpress目录分页
  • 网站的管理更新维护网站后台无法上传图片
  • 视频网站开发流程图网站设计教科书
  • 微信企业网站源码下载互联网实用技术与网页制作书籍
  • 企业网站制作规划网络营销方式和思路
  • 网站及建设中页面廊坊百度推广电话
  • kuler 网站报电子商务( 网站建设与运营)
  • 上海网站公司建设ppt模板下载免费素材网站
  • 网站logo如何替换网站建设买了域名
  • 泽国镇规划建设局网站wordpress上传本地主题
  • 网站建设中静态页面模板广告设计网址
  • 电子商务网站平台建设前景展望导航网站怎么推广
  • 游戏网站做代理肇东网页设计
  • 网站访问速度查询如何建立和设计公司网站作文
  • 网站搭建报价单怎么用群晖做网站
  • 与电子商务网站建设有关实训报告wordpress读取相册
  • 单人给一个公司做网站费用手机芒果tv2016旧版
  • 深圳网站制作联系电话免费优化网站排名