Remember to solve the static resource loading problem in Spring MVC

Original link: https://hsiaofeng.com/archives/232.html

Put static resources in src/main/resources/static , but still get 404 errors when accessing.

 org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping for GET /static/css/bootstrap.min.css

solution

First put static resources in src/main/resources/static .

Create a new configuration class com.demo.config.WebConfig and write the configuration. Be sure to write @EnableWebMvc on the second line below.

 @Configuration @EnableWebMvc public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**") .addResourceLocations("classpath:/static/"); } }

Scan configuration in application-context.xml .

 <context:annotation-config /> <context:component-scan base-package="com.demo.config" />

refer to

ChatGPT.

This article is transferred from: https://hsiaofeng.com/archives/232.html
This site is only for collection, and the copyright belongs to the original author.