How to reference a .css file from a view (.jsp) in Spring MVC?

我只是一个虾纸丫 提交于 2019-12-07 23:46:21

问题


This is the structure: (they're in the same directory!)

Directory
|-view.jsp
|-stylesheet.css

When I do <link href="stylesheet.css" rel="stylesheet" media="screen"> The .css file does not get referenced correctly, i.e. I have no idea what path to set to get to it (if put as a URL in the browser, I get a 404).

I guess it gets translated as http://localhost:8080/myApp/stylesheet.css and then there is no mapping defined for it. Logging says:

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/myApp/stylesheet.css] in DispatcherServlet with name 'appServlet'

It should work like this, shouldn't it? For example, this works:

<%@ include file="include.jsp"%>

include.jsp is in the same folder as well.


回答1:


JSP views and other resources stored under WEB-INF/ are not directly accessible to the end-user, they are web application's private resources and the server doesn't expose them. You'll have to place any public resources one level above WEB-INF/, for example:

webapp/
|-- style/
|     stylesheet.css
|-- images/
|     image1.png
|     image2.png
|-- html/
|     index.html
+- WEB-INF/
  +-- jsp/
        view.jsp
        include.jsp


来源:https://stackoverflow.com/questions/16500861/how-to-reference-a-css-file-from-a-view-jsp-in-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!