引用 JSP 文件中 WEB-INF 文件夹中的资源会在资源上返回 HTTP 404
我有一个名为 BookShopWeb 的动态 Web 项目,我在 eclipse 中创建了它,具有以下目录结构
/BookShopWeb/|
|--src
|---WebContent
|
|---META-INF
|----WEB-INF---web.xml
|
|--css--styles.css
|--jsp---index.jsp
在 web.xml 中,我将起始页设置为
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
在 index.jsp 中,我将 css 包含为
<head>
<link rel="stylesheet" type="text/css" href="../css/styles.css" />
</head>
加载时的索引页,但不显示我用 firebug 检查了该元素,它显示了一个错误报告
Apache Tomcat/6.0.29 - Error report..
The requested resource (/css/styles.css) is not available.
知道为什么会发生这种情况吗?我该如何纠正这个问题? 谢谢 标记
I have a dynamic web project called BookShopWeb which I created in eclipse, with the following directory structure
/BookShopWeb/|
|--src
|---WebContent
|
|---META-INF
|----WEB-INF---web.xml
|
|--css--styles.css
|--jsp---index.jsp
In web.xml I set the start page as
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
In the index.jsp I am including the css as
<head>
<link rel="stylesheet" type="text/css" href="../css/styles.css" />
</head>
The index page when loaded however does not show the css info.I checked the element with firebug and it shows an error report
Apache Tomcat/6.0.29 - Error report..
The requested resource (/css/styles.css) is not available.
Any idea why this occurs?How can I correct this?
thanks
mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
/WEB-INF
文件夹中的文件不可公开访问。将 CSS 文件放在上一层WebContent
文件夹中,并确保可以通过直接在浏览器地址栏中输入其 URL 来访问它们。另外,您在中指定的 URL 必须相对于请求 URL(打开 JSP 时在浏览器地址栏中看到),而不是相对于其在服务器上的位置磁盘文件系统。最好的方法是以正斜杠
/
开头,使其与域相关。或者更动态一些,这样您就不需要每次更改上下文路径时都更改 JSP
JSP 文件可以保存在
/WEB-INF
中,但这样它们只能通过调度 servlet,可以通过扩展 HttpServlet 自行开发,也可以通过 servlet 容器(例如
)隐式地进行调度。另请参阅:
Files in
/WEB-INF
folder are not public accessible. Put the CSS files one level up, in theWebContent
folder, and ensure that they are accessible by entering their URL straight in the browser address bar. Also, the URL which you specify in the<link href>
must be relative to the request URL (which you see in the browser address bar when opening the JSP), not to its location on the server disk file system. The best approach is to make it domain-relative by starting with a forward slash/
.or a bit more dynamically so that you don't need to change your JSPs everytime whenever you change the context path
JSP files can be kept in
/WEB-INF
, but this way they are only accessible through a dispatching servlet, either homegrown by extendingHttpServlet
or implicitly by the servletcontainer such as the<welcome-file>
.See also:
您的目录结构应该是
另外,您将 css 命名为 styles.jsp,这不是声明 css 文件的正确方法。
在您的 web.xml 中:
在您的 jsp 文件中:
Your directory structure should be
Also You named your css as styles.jsp which is not proper way to declare a css file.
In your web.xml:
In your jsp file: