Default static resource supply
SpringBoot has several default static resource directories, which can be configured as well. The default configuration /** maps to /static (or /public , /resources , /META-INF/resources ), and the custom configuration method is as follows:
spring.mvc.static-path-pattern=/** # Path pattern used for static resources.
If the front-end needs to access the default static resources, please pay attention to the following directory structure:
└─resources │ application.yml │ ├─static │ ├─css │ │ index.css │ │ └─js │ index.js │ └─templates index.html
How to reference the above static resources in index.html?
The following writing method:
<link rel="stylesheet" type="text/css" href="/css/index.css" rel="external nofollow" ><script type="text/javascript" src="/js/index.js"></script>
Note: The default configuration /** maps to /static (or /public , /resources , /META-INF/resources)
When requesting /css/index.css, Spring MVC will be found in the /static/ directory.
If configured as /static/css/index.css, then there is no /static directory under the several directories configured above, so the resource file will not be found!
So when writing static resource locations, do not bring the mapped directory names (such as /static/, /public/, /resources/, /META-INF/resources/)!
Custom static resources
The online information says that the specification can be defined in the configuration file. I did not use this method. I used it by implementing extended Configuration.
PS: Explain that in the SpringBoot 1.x version, some configurations related to Spring MVC are extended by inheriting WebMvcAutoConfiguration, but in the 2.x version, the interface WebMvcConfigurer is directly implemented to extend Spring MVC related functions, such as configuring an interceptor, configuring a general return value processor, configuring unified exception handling, etc., of course, it also includes configuring the custom static resource path in this article, covering the default methods such as inside.
Just upload the code:
@Configurationpublic class MyWebAppConfigurer implements WebMvcConfigurer { // event.share.image.dir=/data/share/image/ @Value("${event.share.image.dir}") private String outputDir; @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/share/image/**").addResourceLocations( "file:"+outputDir); }}Note: The background of the above code is an image taken from another place dynamically. It must not be placed in the SringBoot jar package. Therefore, through the above configuration, you can directly access the image in /data/share/image/a.jpg through http://host/share/image/a.jpg. If the static resource file is not dynamic, it is also in the resources directory, just need to write it like this:
registry.addResourceHandler("/share/image/**").addResourceLocations( "classpath:"+outputDir); // Change file to classpathAccessing static resources through SpringBoot tool class
Very simple, the code is as follows:
private static final String BACKGROUND_IMAGE = "share/background.jpg";File file = new ClassPathResource(BACKGROUND_IMAGE).getFile();InputStream is = new ClassPathResource(BACKGROUND_IMAGE).getInputStream();
It turns out that there is another way to write it:
private static final String BACKGROUND_IMAGE = "classpath:share/background.jpg";File file = ResourceUtils.getFile(BACKGROUND_IMAGE);
But in version 2.x, the following may appear but exception
java.io.FileNotFoundException: class path resource [share/background.jpg] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/home/ubuntu/wxcs/calendar-api-1.0.0.jar!/BOOT-INF/classes!/share/background.jpg
I would recommend the first way to write it.