まず、デフォルトの静的リソースマッピング
春ブーツ静的リソースマッピングは、デフォルトの設定を提供します
デフォルトでは、春ブーツ/ **以下のディレクトリへのすべてのアクセスマップ:
classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources
各ブラウザアクセス:
http://localhost:8080/a.jpg
http://localhost:8080/b.jpg
http://localhost:8080/c.jpg
写真は、通常、適切なリソースにアクセスすることができます。だから、直接のリターンがある場合は、適切なリソースが存在するかどうかを見つけるために、一つのパブリックリソース静的なものから春ブートデフォルトを説明します。
カスタム静的リソースマッピング
実際の開発では、カスタム静的リソースへのアクセス・パスが必要になる場合があり、あなたが達成するためにWebMvcConfigurerAdapterを継承することができます。
最初の方法:静的リソース割り当てクラス
package com.sam.demo.conf;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* 配置静态资源映射
* @author sam
* @since 2017/7/16
*/
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//将所有/static/** 访问都映射到classpath:/static/ 目录下
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
}
訪問、プロジェクトを再起動します。http:// localhost:8080 /静的/ c.jpg画像c.jpgリソースを静的なディレクトリへの通常のアクセスの下で。