Static resource processing in Spring Boot practice, springboot
In the first two chapters, we shared Spring boot's support for Restful, but Restful interfaces usually only return data. During web development, we often have a lot of static resources, such as html, images, and css. How can I return static resources to the front end? Those who have worked in web development before should know that there is a webapp directory under the web project we created previously. We only need to put static resources under this directory to directly access it. However, the Spring boot-based project does not have this directory. What should we do?
I. the most stupid way
First, we will share the most stupid way: to directly return static resources to the front end through a stream. We will create an html directory under the root directory of resources of the maven project, then, we put the html file under this directory and specify that any access path starting with/static/accesses the static resources under this directory. Its implementation is as follows:
@Controllerpublic class StaticResourceController { @RequestMapping("/static/**") public void getHtml(HttpServletRequest request, HttpServletResponse response) { String uri = request.getRequestURI(); String[] arr = uri.split("static/"); String resourceName = "index.html"; if (arr.length > 1) { resourceName = arr[1]; } String url = StaticResourceController.class.getResource("/").getPath() + "html/" + resourceName; try { FileReader reader = new FileReader(new File(url)); BufferedReader br = new BufferedReader(reader); StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(line); line = br.readLine(); } response.getOutputStream().write(sb.toString().getBytes()); response.flushBuffer(); } catch (IOException e) { e.printStackTrace(); } }}
The implementation process is very simple, that is, the resource uri is first separated from the path, then the file is read from the static directory and output to the front-end. For simple demonstration, only text files are processed here. image files can be processed similarly. Of course, we certainly won't do this in practice, and Spring boot certainly has a better solution. However, although this method is a bit stupid, it is indeed the most essential thing. No matter how convenient the framework can help us deal with such problems, but aside from the framework, we still need to be able to write a web project skillfully. Only by knowing its implementation principles can you be able to cope with problems. Now let's take a look at Spring boot's support for static resources.
Ii. Default static resource access mode for Spring boot
By default, Spring boot accesses/** directly to files in four directories:
Classpath:/public/
Classpath:/resources/
Classpath:/static/
Classpath:/META-INFO/resouces/
Now we will create the following four directories under the resource file resources directory:
Note that the resources folder under the blue bar is different from the classpath:/resources folder under the class path. The resources under the Blue Bar represent the files under the directory as resource files, during packaging, all files in the directory will be packaged under the class path. This name can be changed in pom. you can specify the Resource Directory in xml:
<resources> <resource> <directory>src/main/resources</directory> </resource></resources>
Resources under the class path is one of the default static resource folders of spring boot, which has the same functions as public, static, and MEAT-INFO/resources. Now we can restart Spring boot through
Http: // localhost: 8080/1. html
Http: // localhost: 8080/2. html
Http: // localhost: 8080/3. html
Http: // localhost: 8080/4. html
The four URLs access the static resources under the four directories.
3. Customize static resource directories
Through section 2, we have learned about the directory of static resources that Spring boot can access by default, but you will surely think, is this directory fixed? Can we customize static resource directories? The answer is yes. Now we will define a static resource directory. We will define an images directory to store images, all/image/** paths access resources in the images directory:
@Configurationpublic class ImageMvcConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/image/**") .addResourceLocations("classpath:/images/"); }}
This code should be relatively simple. @ Configuration identifies a Configuration class, which has been mentioned many times in the previous article. WebMvcConfigurerAdapter is an mvc configuration adapter provided by Spring. There are many configuration methods in it. addResourceHandlers is a method dedicated to handling static resources. We will discuss other methods later. Now we are verifying whether the above configuration is valid. I put a spring.jpg image in the imagesdirectory. Now we can access the image through http: // localhost: 8080/image/spring.jpg:
In fact, in addition to the above method, there is a simpler way, that is, directly configure in application. yml:
spring: mvc: static-path-pattern: /image/** resources: static-locations: classpath:/images/
Static-path-pattern: access mode. The default value is/**. Multiple Access modes can be separated by commas (,).
Static-locations: Resource Directory, separated by commas (,). The default Resource Directory is classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath: /public/
Note: This configuration overwrites the default static Resource Directory of Spring boot. For example, if it is configured in the example, you cannot access resources in the static, public, and resources directories.
Iv. Summary
This article mainly to share with you the Spring boot on the static resources of the processing method, Spring boot by default can access classpath:/META-INF/resources/, classpath:/resources/, classpath:/static /, classpath:/public/static resources under the four directories, we can also customize the configuration as needed. Finally, it should be noted that if the four directories contain resources with the same name, which directory will be preferentially returned? You should be able to guess through the default order of static-locations, by default, Spring boot will first return resources under/META-INF/resources. Of course, because we can customize the value of static-locations, this priority can also be adjusted.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.