1. Overview
Springboot The default static resource access path is: /static or /public or /resources or /META-INF/resources. All addresses must be defined in the src/main/resources directory file. This can automatically load the project static address directory to classpath when the project starts. The static access address is actually loaded into the WebMvcConfigurerAdapter using the ResourceHttpRequestHandler core processor to overwrite the addResourceHandlers method. Redefine the static access directory. We can also implement the method, manually specifying the static access path, rewriting the internal method addResourceHandlers can also achieve the effect we want.
The first method: put it in the src/main/webapp directory
Static resources placed in the webapp directory can be accessed directly
user.html
2.png
Reference 2.png in user.html
The second way: put it under the classpath
Description in ResourceProperties
org.springframework.boot.autoconfigure.web.ResourceProperties private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" };Static resources are placed under the classpath path by default: Defaults to classpath:[/META-INF/resources/,/resources/, /static/, /public/] plus context:/ (the root of the servlet context).
person/index.html
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title><link href="/css/main.css" rel="external nofollow" rel="stylesheet" type="text/css"/><script type="text/javascript" src="/js/main.js"></script><script type="text/javascript"> sayHello();</script></head><body> <h3>person page HTML</h3></body></html>
Set the location of the static resource by modifying the configuration item
application.properties# Modify the default static resource storage directory spring.resources.static-locations=classpath:/web/
Summarize
The above is the method of accessing static resources in SpringBoot introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!