SpringBoot探究Web开发

作者:gqk


SpringBoot开发和传统的SpringMVC开发 目录结构不一样;

1.静态资源处理

静态资源映射规则:

  • SpringBoot中,SpringMVC的web配置都在 WebMvcAutoConfiguration 这个配置类里面;

  • 我们可以去看看 WebMvcAutoConfigurationAdapter 中有很多配置方法;

  • 有一个方法:addResourceHandlers 添加资源处理

 protected void addResourceHandlers(ResourceHandlerRegistry registry) {
            super.addResourceHandlers(registry);
            if (!this.resourceProperties.isAddMappings()) {
//禁用默认资源处理 logger.debug(
"Default resource handling disabled"); } else { ServletContext servletContext = this.getServletContext();
          //此种方式我们可以访问/META-INF/resources/webjars/下面大的静态文件
this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
//此种方式是在指定的静态文件下面查找资源 this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> { registration.addResourceLocations(this.resourceProperties.getStaticLocations()); if (servletContext != null) { registration.addResourceLocations(new Resource[]{new ServletContextResource(servletContext, "/")}); } }); } }

什么是webjars 呢?

WebJars是将客户端(浏览器)资源(JavaScript,Css等)打成jar包文件,以对资源进行统一依赖管理。WebJars的jar包部署在Maven中央仓库上。

webjars静态资源映射规则

https://www.webjars.org

 

 

 我们可以就看到web‘jars帮我们准备了很多打好的静态jar 我们需要使用哪个直接获取就可以

比如我们需要使用jQuery的静态资源、

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.5.1</version>
</dependency>

导入完毕,查看webjars目录结构,并访问Jquery.js文件!

 

 访问:http://localhost:8080/webjars/jquery/3.5.1/jquery.js

 

第二种静态资源映射规则

 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;
        private boolean addMappings;
        private boolean customized;
        private final WebProperties.Resources.Chain chain;
        private final WebProperties.Resources.Cache cache;

        public Resources() {
            this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
            this.addMappings = true;
            this.customized = false;
            this.chain = new WebProperties.Resources.Chain();
            this.cache = new WebProperties.Resources.Cache();
        }

 

ResourceProperties 可以设置和我们静态资源有关的参数;这里面指向了它会去寻找资源的文件夹,即上面数组的内容。

所以得出结论,以下四个目录存放的静态资源可以被我们识别:

"classpath:/META-INF/resources/"
"classpath:/resources/"
"classpath:/static/"
"classpath:/public/"

我们可以在resources根目录下新建对应的文件夹,都可以存放我们的静态文件;

 

 

 自定义静态资源路径

 我们也可以自己通过配置文件来指定一下,哪些文件夹是需要我们放静态资源文件的,在application.properties中配置;

spring.resources.static-locations=classpath:/文件名/,classpath:/文件名/

 首页处理

 

private Resource getWelcomePage() {
            String[] var1 = this.resourceProperties.getStaticLocations();
            int var2 = var1.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                String location = var1[var3];
                Resource indexHtml = this.getIndexHtml(location);
                if (indexHtml != null) {
                    return indexHtml;
                }
            }



//首页最终是存放在静态文件下的任意一个inddex.html
 private Resource getIndexHtml(Resource location) {
            try {
                Resource resource = location.createRelative("index.html");
                if (resource.exists() && resource.getURL() != null) {
                    return resource;
                }
            } catch (Exception var3) {
            }

            return null;
        }

 

 比如我访问 http://localhost:8080/ ,就会找静态资源文件夹下的 index.html

自定义应用图标 

首先禁用SpringBoot默认图标

#关闭默认图标
spring.mvc.favicon.enabled=false

自己放一个图标在静态资源目录下,我放在 public 目录下(注意文件名称必须为favicon.ico)

 

posted @ 2021-02-24 11:38  少侠gqk  阅读(72)  评论(0编辑  收藏  举报