`

项目中 关于相对路径和绝对路径的问题

 
阅读更多

 

 

一般情况下最好用绝对路径 <%=basePath%>/js/jquery.js
其中basePath是下面的值
<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
  + request.getServerName() + ":" + request.getServerPort()
  + path + "/";
%>

为什么要这么做?因为从静态的jsp路径来看它和js的路径相对关系是../js/,但是往往很多时候我们不是直接访问jsp页面的,是通过其他的jsp页面或者servlet,或者struts的action通过forward的方式转发过来访问的,这时候请求的当前路径就不是该jsp的路径,而是转发过来之前那个jsp,servlet或action的路径,所以和js的相对路径关系就可能不再是../js/了,而在实际使用中,访问同一个jsp可能由很多不同的来源,那么它的相对路径关系可能随时都可能改变,这时候jsp页面里写死的相对路径就无法访问到对应的资源了。所以要使用绝对路径访问。

 

例如:
假如我们要访问这个页面,http://localhost:8080/web/jsp/abc.jsp
abc.jsp的相对路径是http://localhost:8080/web/jsp/,abc.jsp里引用了../js/jquery.js,这时候直接访问abc.jsp是没有问题的。
但如果由以下三个请求转发到abc.jsp来访问
1. http://localhost:8080/web/business/test/test.jsp  
相对路径是http://localhost:8080/web/business/test/,访问js需要使用../../js/jquery.js
2. http://localhost:8080/web/struts/action/test.action
相对路径是http://localhost:8080/web/struts/test/,(?)访问js需要使用../../js/jquery.js

相对路径是http://localhost:8080/web/struts/action/ 
3. http://localhost:8080/web/servlet
相对路径是http://localhost:8080/web/,访问js需要使用js/jquery.js

这时候abc.jsp里的../js/jquery.js的死路径就不能正确访问到js了。


===========================================

而真正的相对于web工程的绝对路径写法是:/ 代表url根路径,例如http://localhost:8080/web/js/jquery.js里的http://localhost:8080/,而./代表web工程根路径http://localhost:8080/web/
所以你还可以这么写:
1. /web/js/jquery.js
2. ./js/jquery.js

 

 

建立的目录:

     WebRoot

        1  business-test-test.jsp

        2  js-jquery-1.8.1.js

        3  jsp-abc.jsp

 

代码如下

 

abc.jsp

   <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%
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>
   
    
    <title>My JSP 'abc.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">
	-->
    <script type="text/javascript" src="../js/jquery-1.8.1.js"></script>
  </head>
  
  <body>
         这是abc.jsp 页面 <br>
  </body>
</html>
 

test.jsp

 

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<jsp:forward page="../../jsp/abc.jsp"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'test.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>
  
  <body>
      这是test.jsp页面<br>
  </body>
</html>
 


结果:
    1 由abc.jsp 访问 jquery-1.8.1.js 是没有错的
        http://localhost/RootTest/js/jquery-1.8.1.js
    2 由test.jsp访问abc.jsp 访问 jquery-1.8.1js是有错的 
        访问路径是(http://localhost/RootTest/business/js/jquery-1.8.1.js) 
       正确路径是:http://localhost/business/js/jquery-1.8.1.js

 结论 :显而易见, 项目中用绝对路径好,这样易于管理,而且不会乱套,相对路径修改没及时配置后期很麻烦

http://www.blogjava.net/simie/archive/2007/07/29/133094.html
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics