關于網站開發中連接的可移植性總結

網絡主機地址http://localhost:8080/

1. 相對于根目錄的連接,形如: "/another.jsp"
/DIR/目錄下的
<a href="/another.jsp">Link</a>
<html:link href="/another.jsp">Link</html:link>
<html:link page="/another.jsp">Link</html:link>
在默認ROOT應用中,分別連接到地址:
http://localhost:8080/another.jsp
http://localhost:8080/another.jsp
http://localhost:8080/another.jsp
在應用test中,分別連接到地址:
http://localhost:8080/another.jsp
http://localhost:8080/another.jsp
http://localhost:8080/test/another.jsp
2. 相對于當前目錄的連接,形如: "./another.jsp" 或 "another.jsp"
/DIR/目錄下的
<a href="./another.jsp">Link</a>
<html:link href="./another.jsp">Link</html:link>
<html:link page="./another.jsp">Link</html:link>
在默認ROOT應用中,都分別連接到地址:
http://localhost:8080/
/DIR/another.jsp
http://localhost:8080/
/DIR/another.jsp
http://localhost:8080/
/DIR/another.jsp
在應用test中,分別連接到地址:
http://localhost:8080/
/DIR/another.jsp
http://localhost:8080/
/DIR/another.jsp
http://localhost:8080/test./another.jsp 錯誤連接(而且與DIR無關,都連接到此地址)
/DIR/目錄下的
<a href="another.jsp">Link</a>
<html:link href="another.jsp">Link</html:link>
<html:link page="another.jsp">Link</html:link>
在默認ROOT應用中,都分別連接到地址:
http://localhost:8080/
/DIR/another.jsp
http://localhost:8080/
/DIR/another.jsp
http://localhost:8080/
/DIR/another.jsp
在應用test中,分別連接到地址:
http://localhost:8080/
/DIR/another.jsp
http://localhost:8080/
/DIR/another.jsp
http://localhost:8080/testanother.jsp 錯誤連接(而且與DIR無關,都連接到此地址)
3總結
由于在網站開發時經常不使用默認的WEB應用,而可能把一個模塊開發成一個WEb應用(可能多人合作),
但是在發布的時候要把所有模塊發布為一個WEB應用,并通常是默認的WEB應用,為了使URL不依賴于Web應用
和是否是默認WEB應用,建議如下
a.對于相對于WEB應用根目錄的連接,形如: "/another.jsp"
使用<html:link page="/
..">Link </html:link>
b.對于相對于當前目錄的連接,形如: "./another.jsp" 或 "another.jsp"
使用<html:link href="./another.jsp">Link </html:link>
<a href="./another.jsp">Link </a>或
<html:link href="another.jsp">Link </html:link>
<a href="another.jsp">Link </a>
不要使用<html:link page="./another.jsp">Link </html:link>
<html:link page="another.jsp">Link </html:link> 此二者不可移植
c.建議使用struts的html標簽庫<html:link
..標簽,因為當用戶關閉Cookie時會自動重寫URL,所以概括
兩句話:相對應用根目錄用<html:link page="/.."
相對當前的目錄用<html:link href="./XXXXX.jsp" 或 <html:link href="XXXXX.jsp"
4.補充
還有一個標簽<html:link forward="forwardname"> Link </html:link> 上面沒有提到,現做以說明。
forward屬性和struts配置文件<global-forwards>中的一個<forward>元素匹配,不能和<action>中<forward>匹配。
例子:<global-forwards>
<forward name="forwardname" path="/
/another.jsp"/>
</global-forwards>
<html:link forward="forwardname"> Link </html:link>
相當于<html:link page="/
/another.jsp"> Link </html:link>
需要注意的是:<global-forwards>中<forward>的path要使用相對于WEB應用的根目錄路徑,包括<action>中也是。
在struts1.1多應用模塊時<html:link forwad
. 有時不好使,不知道為什么????(好像是模塊切換狀態
不對)所以最好不用。
替代方法;
網頁中用連接
<html:link page="/forwardaction.do">forward</html:link>
在action-mapping配置如下action
<action path="/forwardaction" forward="/index.jsp" />
posted on 2006-02-06 20:58
java小記 閱讀(210)
評論(0) 編輯 收藏