SpringBoot:Web 开发 ---- 6

内容概览

在这里插入图片描述

sss文档位置:Spring Boot Features ==》7 Developing Web Applications

SpringMVC自动配置概览

sssSpring Boot provides auto-configuration for Spring MVC that works well with most applications.The auto-configuration adds the following features on top of Spring’s defaults:

sss1. Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
sss• 内容协商视图解析器和BeanName视图解析器
sss2. Support for serving static resources, including support for WebJars (covered later in thisdocument)).
sss• 静态资源(包括webjars)
sss3. Automatic registration of Converter, GenericConverter, and Formatter beans.
sss• 自动注册 Converter,GenericConverter,Formatter
sss4. Support for HttpMessageConverters (covered later in this document).
sss• 支持 HttpMessageConverters (后来我们配合内容协商理解原理)
sss5. Automatic registration of MessageCodesResolver (covered later in this document).
sss• 自动注册 MessageCodesResolver (国际化用)
sss6. Static index.html support.
sss• 静态index.html 页支持
sss7. Custom Favicon support (covered later in this document).
sss• 自定义 Favicon
sss8. Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document).
sss• 自动使用 ConfigurableWebBindingInitializer ,(DataBinder负责将请求数据绑定到JavaBean上)

sss自定义配置:

sss1. If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc.
sss • 不用@EnableWebMvc注解。使用 @Configuration + WebMvcConfigurer 自定义规则

sss2. If you want to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, and still keep the Spring Boot MVC customizations, you can declare a bean of type WebMvcRegistrations anduse it to provide custom instances of those components.
sss • 声明 WebMvcRegistrations 改变默认底层组件

sss3. If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc, or alternatively add your own @Configuration-annotated DelegatingWebMvcConfiguration as described in the Javadoc of @EnableWebMvc.
sss • 使用 @EnableWebMvc+@Configuration+DelegatingWebMvcConfiguration 全面接管 SpringMVC

简单功能分析

静态资源访问

sss文档位置: Spring Boot Features ==》7 Developing Web Applications ==》7.1.5 Static Content

sss静态资源目录:

sssBy default, Spring Boot serves static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext. It uses the ResourceHttpRequestHandler from Spring MVC so that you can modify that behavior by adding your own WebMvcConfigurer and overriding the addResourceHandlers method.

sss静态资源放在类路径下:/static (or /public or /resources or /META-INF/resources)

dss[注]:默认情况下,Spring Boot 从类路径中的/static (或/public 或/resources 或/META-INF/resources)目录或 ServletContext 的根目录提供静态内容。它使用 Spring MVC 中的 ResourceHttpRequestHandler,因此您可以通过添加自己的 webmvcconfigureer 和重写resourcehandlers方法来修改该行为。

sssIn a stand-alone web application, the default servlet from the container is also enabled and acts as a fallback, serving content from the root of the ServletContext if Spring decides not to handle it. Most of the time, this does not happen (unless you modify the default MVC configuration), because Spring can always handle requests through the DispatcherServlet.

sssBy default, resources are mapped on /** , but you can tune that with the spring.mvc.static-path-pattern property. For instance, relocating all resources to /resources/** can be achieved as follows:

sss访问 : 当前项目根路径/ + 静态资源名,例如: http://localhost:8080/moon.png

sss原理: 静态映射是 /** ,请求进来,先去找Controller看能不能处理,不能处理的所有请求又都交给静态资源处理器。静态资源处理器会在几个静态资源目录下寻找静态资源。如果静态资源也找不到则响应404页面。

sss改变默认的静态资源路径:

spring:
  resources:
    static-locations: [classpath:/abc/]

在这里插入图片描述
sssWebJars:(这是引入了jquery静态资源的一个访问路径 )

dssssWebJars官网:https://www.webjars.org/
dssss自动映射: /webjars/**
dasdasddasa在这里插入图片描述

dssss访问地址: http://localhost:8080/webjars/jquery/3.5.1/jquery.js 后面地址要按照依赖里面的包路径

sss静态资源访问前缀:

dssss作用:静态资源访问默认无前缀,这样的话我们访问静态资源的时候,请求会被拦截器拦截,因此我们需要增加一个前缀以区分静态资源。

dssss[注]:为了让拦截时能区分出静态资源和动态资源,所以规定静态资源前面加个前缀,拦截器在看到指定前缀时就放行,从而达到动态静态分开的目的

dssss在application.yaml中修改:

欢迎页支持 (默认支持静态和模板两种方式)

sss文档位置: Spring Boot Features ==》7 Developing Web Applications ==》7.1.6 Welcome Page

sss静态资源路径下创建 index.html

sss【注】:

sddsss• 可以配置静态资源路径

sdsdss• 但是不可以配置静态资源的访问前缀。否则导致 index.html不能被默认访问
dasdas在这里插入图片描述

sdsdss• controller能处理 /index.html 请求

自定义 Favicon

dadsdsdsdsdssdsdsdsdsdsdsdsdsdsdsdas在这里插入图片描述
dssss【注1】:我们通过谷歌浏览器,F12 --> Network --> 然后查询Fav…然后找对应的头Headers中的URL获取相应的照片。

dssss【注2】:favicon.ico 直接存在静态资源下(static目录下)即可,失败可能需要禁用缓存,或者因为session原因导致。静态资源前缀也会导致Favicon失效。相应解决方法 ①:禁用缓存。②:换个浏览器(火狐)。③:去掉静态资源前置

spring:
#  mvc:
#    static-path-pattern: /res/**   这个会导致 Favicon 功能失效

dadsdssdsd在这里插入图片描述

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值