Spring无法识别静态资源(样式、字体、jQuery)问题求助
Hey there! Let's work through why your HTML pages aren't loading styles and scripts properly in your Spring app. Based on the code you shared and common Spring pitfalls, here are the most likely fixes:
1. Fix Your Resource Path References
The relative paths you're using (../static/...) are probably the main issue. Spring automatically maps static resources stored in src/main/resources/static (and other default directories) to your app's root URL. That means you don't need to include ../static in your links—just reference them directly from the root:
Before:
<link href="../static/style/styleDistance.css" rel="stylesheet" type="text/css" /> <link href="../static/css/fontello.css" rel="stylesheet" type="text/css" /> <script src="jquery-1.11.3.min.js"></script>
After:
<link href="/style/styleDistance.css" rel="stylesheet" type="text/css" /> <link href="/css/fontello.css" rel="stylesheet" type="text/css" /> <!-- Make sure jQuery is stored in static/js, then reference it like this --> <script src="/js/jquery-1.11.3.min.js"></script>
2. Verify Your Resource Directory Structure
Double-check that your files live in one of Spring's default static resource locations—this is critical for Spring to find them. Spring prioritizes these directories:
src/main/resources/static(the most commonly used location)src/main/resources/publicsrc/main/resources/resourcessrc/main/resources/META-INF/resources
For example, your styleDistance.css should be at src/main/resources/static/style/styleDistance.css to match the corrected path above.
3. Customize Resource Mapping (If Needed)
If your static assets are stored in a non-default directory, you can configure Spring to map them explicitly. Create a web config class:
@Configuration public class CustomWebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // Map URLs starting with /custom-static/ to your custom directory registry.addResourceHandler("/custom-static/**") .addResourceLocations("classpath:/your-custom-directory/"); } }
Then reference your assets using the new mapped path, like /custom-static/style/styleDistance.css.
4. Check Spring Security Restrictions (If Applicable)
If your project uses Spring Security, it might be blocking access to static resources. Update your security config to allow unauthenticated access to your asset paths:
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() // Allow free access to all static asset paths .antMatchers("/style/**", "/css/**", "/js/**").permitAll() // Require authentication for other routes .anyRequest().authenticated(); } }
5. Clear Browser Cache
Sometimes browsers cache old resource versions, making it look like your new styles aren't loading. Force a hard refresh with Ctrl + F5 (Windows/Linux) or Cmd + Shift + R (Mac) to bypass cached files.
内容的提问来源于stack exchange,提问作者Konrad Krawczyk

