Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with static resources of Spring Boot

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to deal with the static resources of Spring Boot". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to deal with the static resources of Spring Boot" can help you solve the problem.

First, the stupidest way

First of all, let's share one of the stupidest ways, that is, to return static resources directly to the front end through the stream. We create a html directory under the root directory of the resources of the maven project, and then we put the html files in this directory and specify that any access path that begins with / static/ will access the static resources in this directory. The 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, which is to 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, so here only deal with the text type of files, picture files can do similar processing. Of course, we certainly won't do this in practice, and Spring Boot certainly has a better solution. However, although this approach is a bit stupid, it is indeed the most essential thing, no matter how convenient the framework is to help us deal with such problems, but apart from the framework, we still have to be able to write a web project skillfully. Only by knowing the principle of its implementation can you be handy when you encounter problems. Now let's take a look at Spring boot's support for static resources.

2. Spring boot default static resource access method

By default, Spring boot can directly access files in four directories by accessing / * *:

Classpath:/public/

Classpath:/resources/

Classpath:/static/

Classpath:/META-INFO/resouces/

Let's now create the following four directories under the resource file resources directory:

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 under the classpath. This name can be changed. You can specify the resource directory in pom.xml:

Src/main/resources

Resources under the classpath is one of the default static resource folders for spring boot, which has the same functions as public, static, and MEAT-INFO/resources. Now we can restart Spring boot by:

Http://localhost:8080/1.html

Http://localhost:8080/2.html

Http://localhost:8080/3.html

Http://localhost:8080/4.html

Four URL have access to static resources in four directories.

Third, customize the static resource directory

Through the second section, we already know the directory of static resources that Spring boot can access by default, but you must wonder, is this directory fixed? Can we define static resource directories ourselves? The answer is yes. We will now customize a static resource directory. We will define an images directory to store images. All / image/** paths will access the 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 previous articles. WebMvcConfigurerAdapter is an adapter provided by Spring to configure mvc. There are many ways to configure it. AddResourceHandlers is a method that specifically deals with static resources. We will talk about other methods later. Now we are verifying that the above configuration is valid. I put a picture of spring.jpg in the images directory, and now we access the picture through http://localhost:8080/image/spring.jpg:

In fact, in addition to the above method, there is a simpler method, which is to configure it directly in application.yml:

Spring: mvc: static-path-pattern: / image/** resources: static-locations: classpath:/images/

Static-path-pattern: access mode. Default is / * *. Multiple static-locations: resource directories can be separated by commas. The default resource directory is classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/. Note, this configuration overrides the default static resource directory of Spring boot. For example, if you configure it as in the example, you can no longer access resources under static, public, resources and other directories.

This is the end of the content about "how to deal with Spring Boot's static resources". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report