引用放置在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>

索引页面在加载时不显示css信息,我用萤火虫检查了该元素并显示了错误报告


Apache Tomcat/6.0.29 - Error report..

The requested resource (/css/styles.css) is not available.

知道为什么会这样吗?如何纠正呢?

元芳怎么了
浏览 765回答 3
3回答

largeQ

文件/WEB-INF夹中的文件不可公共访问。将CSS文件向上一级放置在文件WebContent夹中,并通过直接在浏览器地址栏中输入URL来确保可访问它们。另外,您在中指定的URL <link href>必须相对于请求URL(在打开JSP时在浏览器地址栏中看到),而不是相对于其在服务器磁盘文件系统上的位置。最好的方法是通过以正斜杠开始使其相对于域/。<link rel="stylesheet" href="/BookShopWeb/css/styles.css" />或更具动态性,因此您不必每次更改上下文路径时都更改JSP<link rel="stylesheet" href="${pageContext.request.contextPath}/css/styles.css" />JSP文件可以保留在其中/WEB-INF,但是通过这种方式,它们只能通过分派servlet进行访问,该servlet可以通过扩展来本地生成,也可以由servlet HttpServlet容器(例如)隐式地访问<welcome-file>。

白衣非少年

您的目录结构应为/BookShopWeb/|&nbsp; |--src&nbsp; |---WebContent&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |---META-INF&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |----WEB-INF---web.xml&nbsp; |&nbsp; |--css--styles.css&nbsp; |--jsp---index.jsp&nbsp;另外,您将css命名为styles.jsp,这不是声明css文件的正确方法。在您的web.xml中:<welcome-file-list><welcome-file>index.jsp</welcome-file>在您的jsp文件中:<head><link rel="stylesheet" type="text/css" href="./css/styles.css" /></head>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript