JSP 各种路径问题

landxlost 2011-03-15 01:51:50
JSP 页面在本地运行正常,(http://localhost:8080/项目名/CSS/XXX.CSS 或者图片) 当放到服务器上时, 所以路径全部失效了 (图片 CSS 等等) 该怎么处理呢?
...全文
134 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
LoongMint 2011-03-15
  • 打赏
  • 举报
回复
css样式表里图片路径写相对路径。
landxlost 2011-03-15
  • 打赏
  • 举报
回复
谢谢你们了!

现在的新问题是 在CSS样式表文件中 图片的路径如何写,试了好多方法都不管用,只能在本地运行没错。
智鹿软件 2011-03-15
  • 打赏
  • 举报
回复
每个页面的head里面加上<base href="<%=basePath%>">


不用每一个路径都改的。
智鹿软件 2011-03-15
  • 打赏
  • 举报
回复
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>
hyman_xie 2011-03-15
  • 打赏
  • 举报
回复
路径前都加上${pageContext.request.contextPath}就可以了
s547741891 2011-03-15
  • 打赏
  • 举报
回复
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
landxlost 2011-03-15
  • 打赏
  • 举报
回复
好了,谢谢

还有个问题想请问下: 就是在CSS文件中 很多图片的路径该怎么写啊。 使用绝对定位吗? 这样放到服务器上的时候又要去修改CSS中的图片路径了
hepeng_8 2011-03-15
  • 打赏
  • 举报
回复
注意看看jsp页面最开始的那写语句
LoongMint 2011-03-15
  • 打赏
  • 举报
回复
可以使用
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
在页面中的样式或图片前加上 ${basePath}css/aa.css或<%=basePath %>css/aa.css即可。这样不会失去路径。

81,092

社区成员

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

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