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

What if SpringBoot cannot access static resources under static?

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

Share

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

This article is about how SpringBoot can't access static resources under static. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

SpringBoot cannot access static resources under / static rules for SpringBoot access to static resources

All in the WebMvcAutoConfiguration autoconfiguration class

There are methods for dealing with resources under this class.

By default, static resource files are loaded according to the loading order, but I tried N times, but the browser could not access the resources; after viewing the official website, I changed the default loading location. As follows:

Spring.resources.static-locations=/static/

But still failed to visit as scheduled! Looking back at Web's autoconfiguration class, I found a configuration that was ignored.

In the absence of the class WebMvcConfigurationSupport.class, we will only follow SpringBoot's Web automatic configuration, and in = @ EnableWebMvc==, we are surprised to find it.

The @ EnableWebMvc annotation of our configuration was read in the SpringBoot container.

It imported the WebMvcConfigurationSupport class through @ Import, which caused the WebMvcAutoConfiguration class not to take effect, and my problem was found! Finally, I found the corresponding explanation on the official website of Spring, which can be referred to by Spring MVC Auto Configuration.

Make a brief summary

If the project has a lot of functions, you can use @ EnableWebMvc annotation to fully take over the automatic injection of SpringBoot to SpringMVC, and write some configurations needed for SpringMVC to run by yourself, which will reduce some meaningless performance consumption caused by automatic injection.

However, for projects with more functions, they still use the SpringMVC extension method recommended by SpringBoot-inheriting the WebMvcConfigurer class, so that you can not only implement the WEB components you want to add, but also retain the automatic injection of the underlying SpringBoot.

Problems encountered in pits where SpringBoot2.*.* cannot access static resources

When importing static resources (css, js packages, etc.) and html page templates, static resources cannot be read, which can be summarized as follows:

1. Enter the url that accesses the static resource. A white page error occurs and the static resource cannot be accessed.

2. Enter the url that accesses the html page, and the access is successful, but the layout of the html page is messy and the style cannot be read.

Since styles are placed in static resource folders, since static resources cannot be accessed, html pages naturally cannot read styles. So the problem boils down to "unable to access static resources".

Note: the template engine uses thymeleaf recommended by springboot to render html pages

The solution

Add the following methods to the configuration class

Master: refer to the configuration class written by the following individual for details

@ Configurationpublic class SigninConfig extends WebMvcConfigurationSupport {/ / add access path @ Override protected void addResourceHandlers (ResourceHandlerRegistry registry) {registry.addResourceHandler ("/ static/**") .addResourceLocations ("classpath:/static/");} / / request and page mapping @ Override protected void addViewControllers (ViewControllerRegistry registry) {/ / urlPath: request / / setViewName:html page registry.addViewController ("/") .setViewName ("signin") A rough reason

Springboot has two main mechanisms for automatic configuration of springmvc (currently known), the first of which is related to the question of "why static resources cannot be accessed":

(1) springboot does not allow the components added by the user to work with the automatic configuration, then the components added by the user will overwrite the automatic configuration.

My configuration class inherits WebMvcConfigurationSupport, which is the springmvc automatic configuration class. I guess this class involves the default configuration of the static resource path (without looking at the source code in detail). @ Configuration adds my configuration class to the container, which is equivalent to that my configuration class and the automatic configuration class are in the container, so according to (1), the automatically configured static resource path will naturally fail, so if we do not specify Naturally, static resources cannot be accessed, so we need to reassign the static resource path (which is why the addResourceHandlers method is overridden).

(2) springboot allows users to add components (such as view, that is, the above addViewControllers method) to work with automatic configuration, then the components added by users will exist with automatic configuration.

So here's the point, springboot1. No changes have been made to case (1) in the version, that is, even if you add related components, the default static resource path is still in effect and there is no need to rewrite the path.

And to springboot2. If you add related components, the default static resource path will fail, and if you do not re-specify the static resource path, you will not be able to access the static resource, so take one more step to reset the static resource path. Over.

-follow-up-

Originally was looking at the official website documents, intended to do as the official said, and then curious to delete the original configuration path code, the result can be accessed! There is no white page error!

@ Configurationpublic class SigninConfig extends WebMvcConfigurationSupport {/ / @ Override// protected void addResourceHandlers (ResourceHandlerRegistry registry) {/ / registry.addResourceHandler ("/ static/**") .addResourceLocations ("classpath:/static/"); / / @ Override protected void addViewControllers (ViewControllerRegistry registry) {/ / urlPath: request / / setViewName:html page registry.addViewController ("/"). SetViewName ("signin");}} Thank you for reading! This is the end of the article on "what to do if SpringBoot cannot access static resources under static". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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