栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot:Web 开发 ---- 6

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot:Web 开发 ---- 6

      • 内容概览
      • SpringMVC自动配置概览
      • 简单功能分析
        • 静态资源访问
        • 欢迎页支持 (默认支持静态和模板两种方式)
        • 自定义 Favicon

内容概览

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/397094.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号