Spring 5无XML配置下资源映射失效、资源无法加载求助
Hey there, let's troubleshoot why your static resources aren't loading with Spring 5's XML-free configuration. I've run into this exact problem a handful of times, so here are the key fixes to try:
1. Correct Your Resource Location Configuration
First up, double-check the addResourceLocations value in your configuration class. The path you use depends on where your static resources are actually stored:
- If your resources live in the Maven/Gradle
src/main/resources/resources/directory (under the classpath), you need to prefix the location withclasspath::@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**") .addResourceLocations("classpath:/resources/"); // Note the classpath: prefix } - If your resources are in
src/main/webapp/resources/(the web application root), then your original/resources/path is okay—but make sure the directory actually exists there.
2. Fix JSP Resource References (Use Absolute Paths)
Your JSP uses a relative path (href="resources/..."), which can break depending on the current request's URL hierarchy (e.g., if you're on a URL like /users/detail, the browser will look for /users/resources/... instead of /resources/...).
Fix this by using an absolute path with the context path:
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/vendor/bootstrap/css/bootstrap.min.css">
Alternatively, use Spring's tag library for cleaner path handling:
- Add the taglib directive at the top of your JSP:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> - Use the
<spring:url>tag to generate the correct path:<link rel="stylesheet" type="text/css" href="<spring:url value='/resources/vendor/bootstrap/css/bootstrap.min.css'/>">
3. Verify Your Configuration Class Annotations
Make sure your web configuration class has the right annotations to enable Spring MVC's resource handling:
@Configuration @EnableWebMvc // This is critical for activating WebMvcConfigurer methods public class WebConfig implements WebMvcConfigurer { // Your addResourceHandlers method here }
Without @EnableWebMvc, Spring won't recognize your resource handler configuration.
4. Check DispatcherServlet Mapping
Ensure your DispatcherServlet is mapped to / (not /*), because /* will intercept all requests—including those for static resources—preventing your resource handler from processing them.
Here's how to set this up in your WebApplicationInitializer:
public class AppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.register(WebConfig.class); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(context)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); // Correct mapping } }
Start with these steps—usually, the issue boils down to a mismatched resource location or a relative path in the JSP. Let me know if you still run into trouble!
内容的提问来源于stack exchange,提问作者Sohum Prabhudesai

