求助 java web关于web.xml中welcome-file的配置

likeajin 2011-09-28 04:43:10
如何把非根目录的文件设置为welcome-file?
工程根目录为web,有一个文件夹为granted,内含一个index.jsp
把granted/index.jsp设置为welcome-file会造成
访问http://localhost:8080/web和访问http://localhost:8080/web/granted/index.jsp不同,后者的CSS、图片等路径都不对。求指导
...全文
238 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
likeajin 2011-10-14
  • 打赏
  • 举报
回复
谢谢Rain000,貌似只有你找到了问题的关键!
谢谢!!!
likeajin 2011-10-13
  • 打赏
  • 举报
回复
现在的做法是在登录页面判断cookie,感觉很不舒服,想通过过滤器转向,首页直接设为granted/index.jsp,问题就是css和图片等都变样了,我已经用相对路径了啊~~~
好心的帮看下5楼贴出的代码,莫非我的相对路径是错误的?
感觉welcome-file配置时是相对web根目录,如何设置相对路径中包含granted一层啊??
iCielo 2011-10-13
  • 打赏
  • 举报
回复
css、图片等路径不对,那就应该在jsp里面采用绝对路径吧。
jsp页面头加上
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

然后在要引用的地方使用相对路径
五哥 2011-09-29
  • 打赏
  • 举报
回复
granted/index.jsp中都使用相对路径玩玩 ?
likeajin 2011-09-29
  • 打赏
  • 举报
回复
谁帮看看,谢谢
likeajin 2011-09-29
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lh412552703 的回复:]

"/"貌似有在该目录的上级目录下查找文件.
[/Quote]
那为什么访问http://localhost:8080/web/granted/index.jsp就什么问题都没有呢?
lh412552703 2011-09-29
  • 打赏
  • 举报
回复
"/"貌似有在该目录的上级目录下查找文件.
likeajin 2011-09-29
  • 打赏
  • 举报
回复
我用的应该是相对路径,贴一小段代码
工程路径:
web
|_css
. |_ext.css
. |_all.css
. |...
|_images
. |_extanim32.gif
. |_...
|_granted
. |_index.jsp
. |_global
. . |_index.js
. . |_...


<!-- index.jsp -->
<link rel="stylesheet" type="text/css" href="../css/ext.css" />
<link rel="stylesheet" type="text/css" href="../css/all.css" />
<link rel="stylesheet" type="text/css" href="../css/default.css" />
<link rel="stylesheet" type="text/css" href="../css/index.css" />
……
<body onunload="t.a.unload();">
<div id="loading-mask" class="loading-mask"></div>
<div id="loading" class="loading">
<div>
<img height="32" width="32" src="../images/extanim32.gif"
style="margin-right:8px;float:left;vertical-align:top;" />
</div>
<div>
<br><span style="font-size: 12px; width:150px;">系统初始化...</span>
<br/>
<br><span id="loading-msg" style="font-size: 12px; color:#666666;">加载核心文件……</span>
<br>

</div>
</div>
<script type="text/javascript" src="global/namespace.js"></script>
……
</body>

以上的结构和代码,配置welcome-file为"granted/index.jsp"。
如果访问http://localhost:8080/web/granted/index.jsp,基本没问题。
如果访问http://localhost:8080/web,基本都有问题,代码中的图片"../images/extanim32.gif"也显示不出来,用浏览器的开发人员工具查看CSS,js文件都查阅不到。
查阅global/namespace.js文件,在地址栏中显示的是"http://localhost:8080/web/global/namespace.js",正确的应该是"http://localhost:8080/web/granted/global/namespace.js",少了granted一层。
所有的css、js、图片等和路径相关都少了"granted"一层。

求助
cuijy02 2011-09-29
  • 打赏
  • 举报
回复
相对路径设置的不对吧
scrack 2011-09-29
  • 打赏
  • 举报
回复
相对路径的问题哇
ace62 2011-09-29
  • 打赏
  • 举报
回复
granted/index.jsp文件中引用的图片、js、css等的路径不恰当,应使用相对路径
海子尘 2011-09-28
  • 打赏
  • 举报
回复
用相对路径

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧