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

【Springboot】Web开发(SpringMVC自动配置概述、静态资源访问及配置、欢迎页面、Favicon)

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

【Springboot】Web开发(SpringMVC自动配置概述、静态资源访问及配置、欢迎页面、Favicon)

 学习视频 

尚硅谷雷神SpringBoot2零基础入门springboot全套完整版(spring boot2)

集数:22—25


 学习笔记 

【Java】学习笔记汇总


文章目录
  • 一、SpringMVC自动配置概述
  • 二、静态资源访问
    • 2.1 静态资源目录
      • 2.1.1 静态资源访问前缀
      • 2.1.2 改变默认的静态资源路径
      • 2.1.3 webjar
    • 2.2 欢迎页支持
    • 2.3 自定义 Favicon
    • 2.4 静态资源配置原理

一、SpringMVC自动配置概述

官方文档

Spring 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:
● Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
○ 内容协商视图解析器和BeanName视图解析器

● Support for serving static resources, including support for WebJars .
○ 静态资源(包括webjars)

● Automatic registration of Converter, GenericConverter, and Formatter beans.
○ 自动注册 Converter,GenericConverter,Formatter

● Support for HttpMessageConverters .
○ 支持 HttpMessageConverters

● Automatic registration of MessageCodesResolver .
○ 自动注册 MessageCodesResolver (国际化用)

● Static index.html support.
○ 静态index.html 页支持

● Custom Favicon support.
○ 自定义 Favicon

● Automatic use of a ConfigurableWebBindingInitializer bean .
○ 自动使用 ConfigurableWebBindingInitializer ,(DataBinder负责将请求数据绑定到JavaBean上)


二、静态资源访问

创建Springboot项目,添加以下依赖:

官方文档

2.1 静态资源目录

/static
/public
/resources
/meta-INF/resources

访问: 当前项目根路径/ + 静态资源名

原理:静态映射请求为/**,当有请求进来,先去找Controller看能不能处理;不能处理的所有请求又都交给静态资源处理器;静态资源也找不到则响应404页面

2.1.1 静态资源访问前缀

默认无前缀。

spring:
  mvc:
    static-path-pattern: /res/**

静态资源文件夹下找:当前项目 + static-path-pattern + 静态资源名

2.1.2 改变默认的静态资源路径
spring:
 mvc:
  static-path-pattern: /res/**
 web:
  resources:
   static-locations:
     [ classpath:/haha/]

访问1.jpg:localhost:8080/res/1.jpg

2.1.3 webjar

把常见的静态资源文件转换为jar包,自动映射 /webjars/**

https://www.webjars.org/


    org.webjars
    jquery
    3.5.1

访问地址:localhost:8080/webjars/jquery/3.5.1/jquery.js

后面地址要按照依赖里面的包路径

2.2 欢迎页支持

 方式一

静态资源路径下的index.html

○ 可以配置静态资源路径
○ 但是不可以配置静态资源的访问前缀。否则导致 index.html不能被默认访问

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

  resources:
    static-locations: [classpath:/haha/]

 方式二

通过配置控制器:

@RestController
public class HelloController {
    @RequestMapping("/")
    public String index() {
        return "Welcome!";
    }
}
2.3 自定义 Favicon

favicon.ico 放在静态资源目录下即可。

# 静态页面访问前缀会导致 Favicon 功能失效
# spring:
#  mvc:
#    static-path-pattern: /res/**   
2.4 静态资源配置原理

集数:25

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

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

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