zoukankan      html  css  js  c++  java
  • 13.2 处理静态资源【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/51637052
    目录(?)[-]

    1. 默认资源映射
    2. 自定义资源映射
      1. 自定义目录
      2. 使用外部目录
    3. 通过配置文件配置
      1. 页面中使用
     

    spring Boot 默认为我们提供了静态资源处理,使用WebMvcAutoConfiguration 中的配置各种属性。

    建议大家使用SpringBoot的默认配置方式,如果需要特殊处理的再通过配置进行修改。

    如果想要自己完全控制WebMVC,就需要在@Configuration注解的配置类上增加@EnableWebMvc(@SpringBootApplication注解的程序入口类已经包含@Configuration),增加该注解以后WebMvcAutoConfiguration中配置就不会生效,你需要自己来配置需要的每一项。这种情况下的配置还是要多看一下WebMvcAutoConfiguration类。

    我们既然是快速使用Spring Boot,并不想过多的自己再重新配置。本文还是主要针对Spring Boot的默认处理方式,部分配置在application 配置文件中(.properties 或 .yml)

    默认资源映射

    我们在启动应用的时候,可以在控制台中看到如下信息:

    2016-01-08 09:29:30.362  INFO 24932 ---[           main]o.s.w.s.handler.SimpleUrlHandlerMapping  : MappedURLpath[/webjars/**]ontohandleroftype[class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2016-01-08 09:29:30.362  INFO 24932 ---[           main]o.s.w.s.handler.SimpleUrlHandlerMapping  : MappedURLpath[/**]ontohandleroftype[class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2016-01-08 09:29:30.437  INFO 24932 ---[           main]o.s.w.s.handler.SimpleUrlHandlerMapping  : MappedURLpath[/**/favicon.ico]ont

    其中默认配置的 /** 映射到 /static (或/public、/resources、/META-INF/resources) 
    其中默认配置的 /webjars/** 映射到 classpath:/META-INF/resources/webjars/ 
    PS:上面的 static、public、resources 等目录都在 classpath: 下面(如 src/main/resources/static)。

    如果我按如下结构存放相同名称的图片,那么SpringBoot 读取图片的优先级是怎样的呢? 

    如下图: 

    当我们访问地址 http://localhost:8080/test.jpg 的时候,显示哪张图片?这里可以直接告诉大家,优先级顺序为:META/resources > resources > static >public  (已进行测试)

    如果我们想访问test2.jpg,请求地址 http://localhost:8080/img/test2.jpg

    自定义资源映射

    上面我们介绍了Spring Boot 的默认资源映射,一般够用了,那我们如何自定义目录? 
    这些资源都是打包在jar包中的,然后实际应用中,我们还有很多资源是在管理系统中动态维护的,并不可能在程序包中,对于这种随意指定目录的资源,如何访问?

    自定义目录

    以增加/myres/映射到 classpath:/myres/* 为例的代码处理为: 
    实现类继承 WebMvcConfigurerAdapter 并重写方法 addResourceHandlers (对于

    package org.springboot.sample.config;

     

    import org.springboot.sample.interceptor.MyInterceptor1;

    import org.springboot.sample.interceptor.MyInterceptor2;

    import org.springframework.context.annotation.Configuration;

    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

     

    @Configuration

    public class MyWebAppConfigurer

            extendsWebMvcConfigurerAdapter {

     

        @Override

        public voidaddResourceHandlers(ResourceHandlerRegistry registry) {

            registry.addResourceHandler("/myres/**").addResourceLocations("classpath:/myres/");

            super.addResourceHandlers(registry);

        }

     

    }

    访问myres 文件夹中的test.jpg 图片的地址为 http://localhost:8080/myres/test.jpg 
    这样使用代码的方式自定义目录映射,并不影响Spring Boot的默认映射,可以同时使用。

    如果我们将/myres/修改为 /* 与默认的相同时,则会覆盖系统的配置,可以多次使用 addResourceLocations 添加目录,优先级先添加的高于后添加的。

              

    其中addResourceLocations 的参数是动参,可以这样写addResourceLocations(“classpath:/img1/”, “classpath:/img2/”,“classpath:/img3/”);

    使用外部目录

    如果我们要指定一个绝对路径的文件夹(如 D:/data/api_files ),则只需要使用 addResourceLocations 指定即可。

    // 可以直接使用addResourceLocations 指定磁盘绝对路径,同样可以配置多个位置,注意路径写法需要加上file:

    registry.addResourceHandler("/api_files/**").addResourceLocations("file:D:/data/api_files");

    通过配置文件配置

    上面是使用代码来定义静态资源的映射,其实Spring Boot也为我们提供了可以直接在application.properties(或.yml)中配置的方法。 
    配置方法如下:

     默认值为 /**

    spring.mvc.static-path-pattern=

    # 默认值为classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

    spring.resources.static-locations=这里设置要指向的路径,多个使用英文逗号隔开,

    使用spring.mvc.static-path-pattern 可以重新定义pattern,如修改为 /myres/** ,则访问static 等目录下的fengjing.jpg文件应该为http://localhost:8080/myres/fengjing.jpg ,修改之前为 http://localhost:8080/fengjing.jpg 
    使用 spring.resources.static-locations 可以重新定义 pattern 所指向的路径,支持 classpath: 和 file: (上面已经做过说明) 
    注意 spring.mvc.static-path-pattern 只可以定义一个,目前不支持多个逗号分割的方式。

    页面中使用

    上面几个例子中也已经说明了怎么访问静态资源,其实在页面中使用不管是jsp还是freemarker,并没有什么特殊之处,也我们平时开发web项目一样即可。 

    下面是我的index.jsp:

    <body>

        <imgalt="读取默认配置中的图片"src="${pageContext.request.contextPath}/pic.jpg">

        <br/>

        <imgalt="读取自定义配置myres中的图片"src="${pageContext.request.contextPath}/myres/fengjing.jpg">

    </body>

  • 相关阅读:
    [Js]面向对象基础
    [css]邮件的写法
    [Js]碰撞运动
    [Js]弹性运动
    [Js]布局转换
    [Js]高级运动
    [js]多个物体的运动
    [Js]缓冲运动
    外部 Storage Provider【转】
    hostPath Volume【转】
  • 原文地址:https://www.cnblogs.com/ceshi2016/p/6704703.html
Copyright © 2011-2022 走看看