mar*_*n72 14 resources jsp web-inf
我有一个名为BookShopWeb的动态Web项目,我在eclipse中创建,具有以下目录结构
/BookShopWeb/|
|--src
|---WebContent
|
|---META-INF
|----WEB-INF---web.xml
|
|--css--styles.css
|--jsp---index.jsp
Run Code Online (Sandbox Code Playgroud)
在web.xml中,我将起始页面设置为
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
Run Code Online (Sandbox Code Playgroud)
在index.jsp中,我将css包括在内
<head>
<link rel="stylesheet" type="text/css" href="../css/styles.css" />
</head>
Run Code Online (Sandbox Code Playgroud)
加载时索引页面不会显示css信息.我使用firebug检查了元素,它显示错误报告
Apache Tomcat/6.0.29 - Error report..
The requested resource (/css/styles.css) is not available.
Run Code Online (Sandbox Code Playgroud)
知道为什么会这样吗?我怎么能纠正这个?谢谢你
Bal*_*usC 22
文件/WEB-INF夹中的文件不可公开访问.将CSS文件放在文件WebContent夹中一级,并确保可以通过在浏览器地址栏中直接输入URL来访问它们.此外,您在其中指定的URL <link href>必须相对于请求URL(在打开JSP时在浏览器地址栏中看到),而不是它在服务器磁盘文件系统上的位置.最好的方法是通过正斜杠开始使其成为域相对的/.
<link rel="stylesheet" href="/BookShopWeb/css/styles.css" />
Run Code Online (Sandbox Code Playgroud)
或者更动态一点,这样每次更改上下文路径时都不需要每次都更改JSP
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/styles.css" />
Run Code Online (Sandbox Code Playgroud)
JSP文件可以保存在其中/WEB-INF,但这样它们只能通过调度servlet访问,可以HttpServlet通过servletcontainer 扩展或隐式生成,例如<welcome-file>.
你的目录结构应该是
/BookShopWeb/|
|--src
|---WebContent
|
|---META-INF
|----WEB-INF---web.xml
|
|--css--styles.css
|--jsp---index.jsp
Run Code Online (Sandbox Code Playgroud)
另外,您将css命名为styles.jsp,这不是声明css文件的正确方法.
在您的web.xml中:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
Run Code Online (Sandbox Code Playgroud)
在你的jsp文件中:
<head>
<link rel="stylesheet" type="text/css" href="./css/styles.css" />
</head>
Run Code Online (Sandbox Code Playgroud)