2012-04-26 62 views
4

我想檢索web應用程序的根路徑,然後我想追加鏈接到根路徑。 我試過request.context,但它返回「http:// localhost:8080/webapp/web-inf」。我應該如何獲取jsp頁面中的根文件夾路徑

例如我的根文件夾路徑是

path = http://localhost:8080/webapp/ 

,我想剩下的鏈接添加到該路徑

helpPath= /help/page/help.htm 


<a href="${path} + ${helpPath}" target="_blank">name</a> 

任何幫助或指針非常讚賞。

+0

相關:http://stackoverflow.com/questions/3655316/browser-cant-access-css-and-images-when-calling-a-servlet-which-forwards-to-aj/3658735#3658735 – BalusC 2012-04-26 06:00:25

回答

8
<%=request.getContextPath()%> 

會給你應用程序的ROOTPATH所以你的情況這將是http://localhost:8080/webapp

按照評論:

<%=request.getContextPath()%>/help/page/help.htm 

會給你你的頁面

+2

它返回「/ webapp /」而不是「http:// localhost:8080/webapp /」 – Raje 2012-04-26 05:05:43

+0

這就是你需要的,所以如果你說<%= request.getContextPath()%>/help/page/help.htm你將能夠導航到你的頁面 – 2012-04-26 05:08:44

+0

我記下以下代碼name它的工作原理。感謝您的詳細信息幫助。 – Raje 2012-04-26 05:13:37

13

您可以使用pageContext.request.contextPath

${pageContext.request.contextPath} 

所以,你可以使用,

<a href="${pageContext.request.contextPath}${helpPath}" target="_blank">name</a> 

但更好的方法是將基本href設置到這個路徑,然後,因爲它是使用路徑。

<head> 

     <base href="${pageContext.request.contextPath}"> 

    </head> 
<body> 
<a href="${helpPath}" target="_blank">name</a> 

</body> 
+0

'$ {pageContext.request.contextPath} + $ {helpPath}'不會連續 – tusar 2012-04-26 05:08:24

+0

啊!正確地複製用戶代碼。 Shigh – 2012-04-26 05:14:23

+0

很好的答案。基本href只能是絕對的(即以'http://'開頭)。否則,舊的IE版本可能會做奇怪的事情。 – BalusC 2012-04-26 06:02:32

0

<%=request.getContextPath()%>會給/webapp

所以你的鏈接應該是這樣的:

<a href="<%=request.getContextPath()%>${helpPath}" target="_blank">name</a> 
1

您還可以使用

<c:url> 

JSTL標記。它會爲你添加上下文路徑。

相關問題