Spring can't load my css files

2020-03-27 05:19发布

Spring can't load my css files and this is my architecture:

  • Project
    • Webcontent
    • WEB-INF
      • pages
        • index.jsp
      • css
        • jquery-ui.css

2条回答
Lonely孤独者°
2楼-- · 2020-03-27 05:56

You can put the css file and other files like javascript, images in the folder resources
Project

Webcontent
resources  
     css
         jquery-ui.css
WEB-INF
   pages
     index.jsp

And put the following line in spring configuration

<mvc:resources mapping="/resources/**" location="/resources/" />  

And give the reference in jsp pages like

<link href="${pageContext.request.contextPath}/resources/css/jquery-ui.css"
    rel="stylesheet">
查看更多
▲ chillily
3楼-- · 2020-03-27 05:58

As fvu explained, the content inside WEB-INF is not accessible over HTTP. The Webcontent directory in your directory structure looks like it was created for this particular purpose: storing CSS files, images and JavaScript files.

Even after you move your CSS files, they will not be accessible over HTTP automatically because Spring MVC will intercept these HTTP requests. You need to use the <mvc:resources> tag in your bean configuration:

<mvc:resources mapping="/webcontent/**" location="/Webcontent/" />

Your CSS files will be accessible at URLs like the following:

http://localhost:8080/you-app-name/webcontent/...

There is a lot of information on <mvc:resources> at Stackoverflow.

查看更多
登录 后发表回答