Web开发

导入静态资源

WebMvcAutoConfiguration.class

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});
            }

        });
    }
}

WebProperties.class

public String[] getStaticLocations() {
    return this.staticLocations;
}
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
private String[] staticLocations;
  • webjars是springboot存放静态资源的一种方式(不推荐),可以在webjars网站中找到对应资源的坐标,在pom.xml中添加依赖即可在项目中访问。

  • 在springboot中可以使用以下方式处理静态资源:

  1. webjars (不推荐) localhost:8080/webjars/...

  2. /** src/main/resources/public ,static ,resources localhost:8080/...

  3. 优先级:resources > static > public

  4. 自定义静态资源位置(不推荐)

    application.yaml文件: spring.mvc.static-path-pattern=/hello/, classpath:/my/

首页

在静态资源目录下,创建index.html,即可识别为首页;可放在public和static目录下,不可放在resources目录下

在static目录在创建favicon.ico文件即可设置首页图标

posted @ 2022-04-19 20:03  清水煮岁月  阅读(20)  评论(0编辑  收藏  举报