- 1.配置文件:
- 2.静态资源的配置:
- 2.1 静态资源目录:
- 2.2 欢迎页设置:
- 5. 遇到的问题:
yaml:意思是:“Yet Another Markup Language”(仍是一种标记语言)。 非常适合用来做以数据为中心的配置文件
基本语法:
● k: v:kv之间有空格隔开
● 大小写敏感
● 用缩进表示层级关系,并且缩进不允许用tab,要使用空格
● 缩进的空格数不重要,只要相同层级的元素左对齐即可
● '#‘表示注释
● 字符串无需加引号,如果要加,’'与""表示字符串内容 会被 不转义/转义
数据类型:
●基本数据类型、string都是直接写即可
● 对象(map、hash、object) :用花括号或者缩进表示
● 数组(list、set):用方括号或者-表示
注:
1.对需要配置数值的实体类上要添加@ConfigurationProperties(prefix = “person”),不然会报错。
2.对于字符串可以加引号,但对于一些转义字符(比如 /n),双引号会识别这些转义字符,而单引号则不会识别,会将其当成字符串来看。
只要静态资源放在类路径下: called /static (or /public or /resources or /meta-INF/resources
访问 : 当前项目根路径/ + 静态资源名
遇到的问题:无法访问静态资源报404
解决方法:clean一下maven
原理: 静态映射/**。
请求进来,先去找Controller看能不能处理。不能处理的所有请求又都交给静态资源处理器。静态资源也找不到则响应404页面,如下返回的不再是图片而是aaa
@RequestMapping("/wz5.jpg")
public String toPic(){
return "aaa";
}
修改默认的静态资源路径:
- 添加前缀:以后访问静态资源:项目路径+前缀名+资源名(http://localhost:8080/res/log.PNG)
spring:
mvc:
static-path-pattern: /res/**
- 添加自定义静态资源文件夹:
spring:
web:
resources:
static-locations: [classpath:/haha/]
webjar:
自动映射 /webjars/**
https://www.webjars.org/
org.webjars jquery 3.5.1
访问地址:http://localhost:8080/webjars/jquery/3.5.1/jquery.js 后面地址要按照依赖里面的包路径
2.2 欢迎页设置:在static文件夹或templated文件夹下放置index.html会自动将其设置为欢迎页(即直接输入http://localhost:8080/即可访问)
Spring Boot supports both static and templated welcome pages. It first looks for an index.html file in the configured static content locations. If one is not found, it then looks for an index template. If either is found, it is automatically used as the welcome page of the application.
问题:因为配置了如下,导致报404错误
- 隔了一段时间没有复习springboot导致很多基础的东西全忘记了,比如pojo类前要加@Component 将此类加载进容器



