如何从 Spring MVC 的视图 (.jsp) 中引用 .css 文件? [英] How to reference a .css file from a view (.jsp) in Spring MVC?
问题描述
这是结构:(它们在同一个目录中!)
This is the structure: (they're in the same directory!)
Directory
|-view.jsp
|-stylesheet.css
当我做 .css 文件没有被正确引用,即我不知道要设置什么路径才能访问它(如果将其作为 URL 放在浏览器中,我会得到 404).
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).
我猜它被翻译成 http://localhost:8080/myApp/stylesheet.css
然后没有为它定义映射.日志记录说:
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 也在同一个文件夹中.
include.jsp is in the same folder as well.
推荐答案
JSP
视图和其他存储在 WEB-INF/
下的资源最终无法直接访问-用户,它们是 Web 应用程序的私有资源,服务器不会公开它们.您必须将任何公共资源置于 WEB-INF/
之上一级,例如:
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
这篇关于如何从 Spring MVC 的视图 (.jsp) 中引用 .css 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!