You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

1. Using the resources folder for CSS styles in an IntelliJ Spring MVC project

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 mentioned web.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 actual src/main/webapp/resources folder 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 at src/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 your src/main/webapp folder 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.

2. Is the IntelliJ highlight error for Bootstrap CSS files a real issue?

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 21:37:35