Static resource processing in spring boot practice
In the first two chapters, we shared spring boot's support for restful, but restful interfaces usually only return data. When doing web development, we often have a lot of static resources, such as HTML, pictures, CSS and so on. How to return static resources to the front end? Students who have done web development before should know that there will be a webapp directory under the web project we created before. We can access it directly as long as we put the static resources in this directory. However, projects based on spring boot do not have this directory. What should we do?
1、 The stupidest way
First, let's share the stupidest way, which is to directly return the static resources to the front end through the stream. We establish an HTML directory under the root directory of resources of Maven project, and then we put the HTML file in this directory, and specify that any access path starting with / static / accesses the static resources in this directory. The implementation is as follows:
The implementation process is very simple, that is, first separate the resource URI from the path, then read the file from the static directory and output it to the front end. Because it is only a simple demonstration, only text files are processed here, and image files can be processed similarly. Of course, we certainly won't do this in practice, and spring boot must have a better solution. However, although this method is a little stupid, it is the most essential thing. No matter how convenient the framework helps us deal with this kind of problem, we still need to be able to skillfully write a web project without the framework. Only by knowing its implementation principle can you be handy when you encounter problems. Now let's look at spring boot's support for static resources.
2、 Spring boot default static resource access method
Spring boot accesses / * * by default. You can directly access files in four directories:
classpath:/public/
classpath:/resources/
classpath:/static/
classpath:/Meta-INFO/resouces/
We will now create the following four directories under the resources directory of the resource file:
Note that the resource folder resources under the blue bar is different from the folder classpath: / resources under the classpath. The resources under the blue bar means that the files in this directory are resource files. When packaging, all the files in this directory will be packaged. This name can be changed in POM XML to specify the resource directory:
Resources under the classpath 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
Four URLs access the static resources in four directories.
3、 Custom static resource directory
Through the second section, we have known the directory of static resources that can be accessed by spring boot by default, but you will certainly think, is this directory fixed? Can we define our own static resource directory? The answer is yes. Now we define a static resource directory. We define an images directory to store images. All / image / * * paths will access the resources in the images Directory:
This code should be relatively simple, @ configuration identifies a configuration class, which has been mentioned many times in the previous article. Webmvcconfigureradapter is an adapter provided by spring to configure MVC. There are many configuration methods. Addresourcehandlers is a special method to deal with static resources. We will talk about other methods later. Now we are verifying whether the above configuration is valid. I put a spring. Com in the images directory Jpg pictures, now we pass http://localhost:8080/image/spring.jpg To access pictures:
In fact, in addition to the above methods, there is a simpler method, which is directly in application You can configure it in YML:
Static path pattern: access mode. The default is / * *. Multiple can be separated by commas
Static locations: resource directory. Multiple directories are separated by commas. The default resource directory is classpath: / meta inf / resources /, classpath: / resources /, classpath: / static /, classpath: / public/
Note that this configuration will override the default static resource directory of spring boot. For example, if you configure as in the example, you can no longer access resources in static, public, resources and other directories.
4、 Summary
This article mainly tells you how spring boot handles static resources. Spring boot can access static resources in classpath: / meta inf / resources /, classpath: / public / by default. We can also configure them according to our own needs. Finally, it should be noted that if there are resources with the same name in these four directories, which directory will be returned first? You can guess from the default value sequence of static locations. By default, spring boot will give priority to returning 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 the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.