IntelliJ中Spring MVC项目resources文件夹配置及Bootstrap CSS引用路径疑问
Hey there! Let's work through your two Spring MVC questions step by step— I’ve run into similar quirks with IntelliJ before, so I’ve got you covered.
First off, Spring MVC by default intercepts all incoming requests (thanks to the DispatcherServlet), so you need to explicitly configure it to serve static resources like CSS files. Here’s how to do it:
Add static resource mapping to your Spring configuration:
If you’re using XML config (since you mentionedweb.xml), open your spring-servlet.xml file and add this block:<mvc:resources mapping="/resources/**" location="/resources/"/>This tells Spring to map any request starting with
/resources/to the actualsrc/main/webapp/resourcesfolder in your project.Reference the CSS correctly in your view files:
In your JSP/HTML views, use an absolute path to link the CSS. For example, if your CSS is atsrc/main/webapp/resources/css/style.css, your link tag should look like this:<link rel="stylesheet" type="text/css" href="/resources/css/style.css">If your app is deployed under a context path (like
http://localhost:8080/myapp), use JSTL’s<c:url>tag to auto-include the context path:<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/style.css"/>">Double-check IntelliJ settings:
Make sure yoursrc/main/webappfolder is marked as a Web Resource Directory (right-click it → Mark Directory as → Web Resource Directory). This helps IntelliJ recognize the folder as the root for web assets.
Short answer: No, that highlight is not a critical error— it’s just IntelliJ’s static code analysis getting confused by relative paths, not a problem with your actual project setup.
Here’s what’s happening:
When you place CSS files in src/main/webapp/resources, but your views are in WEB-INF/views, if you use a relative path like ../resources/css/bootstrap.css in your view, IntelliJ might not resolve that path correctly in its editor (even though it will work fine at runtime once Spring handles the mapping). That’s why it throws a highlight warning and seems to want the CSS in WEB-INF/views/resources.
You don’t need to duplicate your CSS files! Instead, fix the highlight by:
- Using the absolute path with
<c:url>as I mentioned earlier. IntelliJ will recognize this path correctly once you set up the static resource mapping. - Or, adjust IntelliJ’s inspection settings: Go to File → Settings → Editor → Inspections → HTML → Unresolved file reference, and either lower the severity of this inspection or exclude your view files from it if you know the paths are correct.
At runtime, as long as your Spring static resource mapping is set up properly, the CSS will load from webapp/resources without any issues— the duplicate files are completely unnecessary.
内容的提问来源于stack exchange,提问作者SuminKim

