2021-06-06

一、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帮组我们进行扩展配置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值