• 7.SpringBoot 之 Web



    添加资源处理
    package org.springframework.boot.autoconfigure.web.servlet.
    public class WebMvcAutoConfiguration {
      private final ResourceProperties resourceProperties;
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
                if (!this.resourceProperties.isAddMappings()) {
                    logger.debug("Default resource handling disabled");
                } else {
                    Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
                    CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
                    if (!registry.hasMappingForPattern("/webjars/**")) {
                        this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"})//解析此路径下的静态资源文件
                  .addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"})          //解析此路径下的静态资源文件
                  .setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl)); } String staticPathPattern = this.mvcProperties.getStaticPathPattern(); if (!registry.hasMappingForPattern(staticPathPattern)) { this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern})
                  .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
                  .setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl)); } } }   }

    欢迎页

         @Bean
            public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
                return new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext),
               applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());//1.欢迎页所在的静态资源访问根路径:/** } static String[] getResourceLocations(String[] staticLocations) { String[] locations = new String[staticLocations.length + WebMvcAutoConfiguration.SERVLET_LOCATIONS.length]; System.arraycopy(staticLocations, 0, locations, 0, staticLocations.length); System.arraycopy(WebMvcAutoConfiguration.SERVLET_LOCATIONS, 0, locations, staticLocations.length, WebMvcAutoConfiguration.SERVLET_LOCATIONS.length); return locations; } private Optional<Resource> getWelcomePage() { String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations());//2.静态页所在的根路径下的路径 return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst(); } private Resource getIndexHtml(String location) { return this.resourceLoader.getResource(location + "index.html");//3.欢迎页的默认文件名叫index.html }
    //1.欢迎页所在的静态资源访问根路径:/** 

    //2.静态页所在的根路径下的路径
    @ConfigurationProperties(
        prefix = "spring.resources",
        ignoreUnknownFields = false
    )
    public class ResourceProperties {
        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 final ResourceProperties.Chain chain; private final ResourceProperties.Cache cache; public ResourceProperties() { this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS; this.addMappings = true; this.chain = new ResourceProperties.Chain(); this.cache = new ResourceProperties.Cache(); } public String[] getStaticLocations() { return this.staticLocations; }

       

        

     欢迎页

                        

    1、WebJars介绍

    Web前端使用了越来越多的JS或CSS,如jQuery,Backbone.js和Bootstrap。一般情况下,我们是将这些Web资源拷贝到Java Web项目的webapp相应目录下进行管理。这种通过人工方式管理可能会产生版本误差,拷贝版本错误,漏拷等现象,导致前端页面无法正确展示,版本不一致,文件混乱等,导致出现一些莫名其妙的错误等。

    WebJars是将web前端资源(js,css等)打成jar包文件,然后借助Maven工具,以jar包形式对web前端资源进行统一依赖管理,保证这些Web资源版本唯一性。WebJars的jar包部署在Maven中央仓库上。

    WebJars官网:https://www.webjars.org/

     

     
    2、SpringBoot使用WebJars

     



    页面图标

    public class WebMvcAutoConfiguration {
         @Configuration
            @ConditionalOnProperty(
                value = {"spring.mvc.favicon.enabled"},
                matchIfMissing = true
            )
            public static class FaviconConfiguration implements ResourceLoaderAware {
                private final ResourceProperties resourceProperties;
                private ResourceLoader resourceLoader;
    
                public FaviconConfiguration(ResourceProperties resourceProperties) {
                    this.resourceProperties = resourceProperties;
                }
    
                public void setResourceLoader(ResourceLoader resourceLoader) {
                    this.resourceLoader = resourceLoader;
                }
    
                @Bean
                public SimpleUrlHandlerMapping faviconHandlerMapping() {
                    SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
                    mapping.setOrder(-2147483647);
                    mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
                    return mapping;
                }
    
                @Bean
                public ResourceHttpRequestHandler faviconRequestHandler() {
                    ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
                    requestHandler.setLocations(this.resolveFaviconLocations());
                    return requestHandler;
                }
    
                private List<Resource> resolveFaviconLocations() {、
              //"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"
               String[] staticLocations = WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.getResourceLocations(this.resourceProperties.getStaticLocations());
                    List<Resource> locations = new ArrayList(staticLocations.length + 1);
                    Stream var10000 = Arrays.stream(staticLocations);
                    ResourceLoader var10001 = this.resourceLoader;
                    this.resourceLoader.getClass();
                    var10000.map(var10001::getResource).forEach(locations::add);
                    locations.add(new ClassPathResource("/"));
                    return Collections.unmodifiableList(locations);
                }
            }
    }

     

    自定义静态资源路径
     
    <link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.1.3/css/bootstrap.css}" rel="stylesheet">
    <link href="asserts/css/dashboard.css" th:href="@{/asserts/css/dashboard.css}" rel="stylesheet">
    <script type="text/javascript" src="asserts/js/jquery-3.2.1.slim.min.js" th:src="@{/webjars/jquery/3.3.1-1/jquery.js}"></script>
    <script type="text/javascript" src="asserts/js/popper.min.js" th:src="@{/webjars/popper.js/1.14.4/popper.js}"></script>
    <script type="text/javascript" src="asserts/js/bootstrap.min.js" th:src="@{/webjars/bootstrap/4.1.3/js/bootstrap.js}"></script>
    <script type="text/javascript" src="asserts/js/feather.min.js" th:src="@{/asserts/js/feather.min.js}"></script>
    <script type="text/javascript" src="asserts/js/Chart.min.js" th:src="@{/asserts/js/Chart.min.js}"></script>
    如果不写th:src  或者th:href 去连接获取,若URL请求路径是多级的,eg:http://localhost:8080/emp/add
    那么资源文件就会去/emp/asserts/js/下去寻找。寻找不到就会报错了。



  • 相关阅读:
    JQuery源码解析(九)
    JQuery源码分析(八)
    C#的扩展方法解析
    JQuery基础DOM操作
    Ajax中的eval函数的用法
    EF上下文管理
    Asp.Net请求管道中的19个事件
    JQuery源码分析(七)
    SoftReference、WeakReference、PhantomRefrence分析和比较
    php 计算gps坐标 距离
  • 原文地址:https://www.cnblogs.com/guchunchao/p/9892467.html
Copyright © 2020-2023  润新知