这是我参与11月更文挑战的第9天,活动详情查看:2021最后一次更文挑战 第一步就是创建一个SpringBoot项目,我们之前说过就不在列举了。
静态资源的映射规则
所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源;
webjars:以jar包的方式引入静态资源;
我们在pom文件中引入jar包
这个一个web的资源网站:www.webjars.org/
我们启动项目去试一下
我们可以通过该路径去访问到我们的文件
"/**" 访问当前项目的任何资源,都去(静态资源的文件夹)找映射
- "classpath:/META-INF/resources/",
- "classpath:/resources/",
- "classpath:/static/",
- "classpath:/public/"
欢迎页; 静态资源文件夹下的所有index.html页面;被"/**"映射;
localhost:8080/ 找index页面
模板引擎
SpringBoot推荐的Thymeleaf;
语法更简单,功能更强大;
在pom文件中导入
只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染;
Thymeleaf语法
我们先创建一个html文件
在文件中导入名称空间
<!DOCTYPE html>
<!--命名空间-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>成功!</h1>
<!--th:text 将div里面的文本内容设置为 -->
<div th:text="${hello}">这是显示欢迎信息</div>
</body>
</html>
编写一个测试代码
@Controller
public class HelloController {
@RequestMapping("/success")
public String success(Map<String,Object> map){
map.put("hello","sadasdasdasdasdasdasedasd");
return "success";
}
}
th:text;改变当前元素里面的文本内容;
th:任意html属性;来替换原生属性的值
SpringMVC自动配置
官方网址:docs.spring.io/spring-boot…
Spring Boot 自动配置好了SpringMVC
\
以下是SpringBoot对SpringMVC的默认配置:* (WebMvcAutoConfiguration)*
\
- Inclusion of
ContentNegotiatingViewResolverandBeanNameViewResolverbeans.
-
- 自动配置了ViewResolver(视图解析器:根据方法的返回值得到视图对象(View),视图对象决定如何渲染(转发?重定向?))
- ContentNegotiatingViewResolver:组合所有的视图解析器的;
-
- 如何定制:我们可以自己给容器中添加一个视图解析器;自动的将其组合进来;
- Support for serving static resources, including support for WebJars (see below).静态资源文件夹路径,webjars
- Static
index.htmlsupport. 静态首页访问
- Custom
Faviconsupport (see below). favicon.ico - 自动注册了 of Converter, GenericConverter, Formatter beans.
-
- Converter:转换器; public String hello(User user):类型转换使用Converter
- Formatter 格式化器; 2017.12.17===Date;
自己添加的格式化器转换器,我们只需要放在容器中即可
- Support for HttpMessageConverters (see below).
-
- HttpMessageConverter:SpringMVC用来转换Http请求和响应的;User---Json;
- HttpMessageConverters 是从容器中确定;获取所有的HttpMessageConverter;
-
- 自己给容器中添加HttpMessageConverter,只需要将自己的组件注册容器中(@Bean,@Component)
- Automatic registration of MessageCodesResolver (see below).定义错误代码生成规则
- Automatic use of a ConfigurableWebBindingInitializer bean (see below).
我们可以配置一个ConfigurableWebBindingInitializer来替换默认的;(添加到容器)
以上是我们对官方文档的一个解读,接下来我们写一个,试验一下
创建如下图
编写一个配置类(@Configuration),是WebMvcConfigurerAdapter类型;不能标注@EnableWebMvc;
既保留了所有的自动配置,也能用我们扩展的配置;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/web").setViewName("success");
}
}
原理:
1)、WebMvcAutoConfiguration是SpringMVC的自动配置类
2)、在做其他自动配置时会导入;@Import(EnableWebMvcConfiguration.class)
3)、容器中所有的WebMvcConfigurer都会一起起作用;
4)、我们的配置类也会被调用;
效果:SpringMVC的自动配置和我们的扩展配置都会起作用;
全面接管SpringMVC
SpringBoot对SpringMVC的自动配置不需要了,所有都是我们自己配置;所有的SpringMVC的自动配置都失效了
我们需要在配置类中添加@EnableWebMvc即可;
这个时候我们就不能直接使用了,就要像原来一样自己去配置视图
@EnableWebMvc将WebMvcConfigurationSupport组件导入进来;导入的WebMvcConfigurationSupport只是SpringMVC最基本的功能;
如何修改SpringBoot的默认配置
1)、SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component)如果有就用用户配置的,如果没有,才自动配置;如果有些组件可以有多个(ViewResolver)将用户配置的和自己默认的组合起来;
2)、在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展配置
3)、在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置