一、springboot中的web开发
1.自动配置
2.springboot对静态资源的规则
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
registration.addResourceLocations(this.resourceProperties.getStaticLocations());
if (this.servletContext != null) {
ServletContextResource resource = new ServletContextResource(this.servletContext, "/");
registration.addResourceLocations(new Resource[]{resource});
}
});
}
}
所有"/webjars/**",都去/META-INF/resources/webjars/找静态资源
webjars:以jar包的方式打包
以引入的jquery为例:
<!-- 引入jquery的webjars-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
直接在local host:8080/webjars/jquery/3.31/jquery.js直接访问。
2."/**"房屋内当前项目的任何资源
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
/ 当前项目的跟路径。
# 定义静态资源文件夹
spring.web.resources.static-locations=
3.自定义扩展springmvc的功能
配置了@Configuration,既保留了默认配置,也可以使用我们扩展的配置。
/**
* @author 赵奥林
* @date 2021年07月18日 15:36
* @Description 使用WebMvcConfigurerAdapter可以来扩展springmvc的功能
*/
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//super.addViewControllers(registry);
//浏览器发送请求zal到success.html页面
registry.addViewController("/zal").setViewName("success");
}
}
4.全面接管springmvc
springboot对springmvc的自动配置不需要了,所有的都是我们自己配置;
我们只需要在配置类中添加@EnableWebMvc即可。
@EnableWebMvc全面接管springmvc,所有springmvc默认配置不生效。
原理:
@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();
public DelegatingWebMvcConfiguration() {
}
springmvc导入了WebMvcConfigurationSupport组件,导致自动配置类失效。
5.修改springboot的默认配置
1.springboot会配置很多组件,先看看容器中有没有自己配置的,如果有就使用用户配置的的,没有就是自动配置,组件有多个,就使用用户和自动配置结合。
2.springboot中又很多的xxxConfigurer帮组我们进行扩展配置。