天天看點

修改Eclipse中Web項目的jsp檔案表頭,自動加上basePath的原因及方法

在myeclipse IDE中有自動添加basePath的jsp檔案; 而在eclipse中需要修改設定IDE,修改Eclipse中Web項目的jsp檔案表頭,使其自動加上basePath

<%  
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
           

使用以上代碼原因:

以上語句用來拼裝目前網頁的相對路徑.

頁面内有一個連接配接,完整的路徑應該是 http://localhost:8080/demo/bbs/css/babasport.css

其中:

http://localhost:8080是伺服器的基本路徑

demo是目前應用程式的名字

根路徑是http://localhost:8080/demo/

是以. 我們設定basePath,使其代表http://localhost:8080/demo/

以後http://localhost:8080/demo/bbs/css/babasport.css等價于<%=basePath%>bbs/css/babasport.css (伺服器會自動把 <base …>指定的路徑和頁面内的相對路徑拼裝起來,組成完整路徑。)

request.getSchema()可以傳回目前頁面使用的協定,http
request.getServerName()可以傳回目前頁面所在的伺服器的名字,localhost
request.getServerPort()可以傳回目前頁面所在的伺服器使用的端口,8080
request.getContextPath()可以傳回目前頁面所在的應用的名字,demo
四個拼裝起來,就是目前應用的根路徑了
即:  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
           

Eclipse如何一次性加上basePath

  • 步驟一:在菜單欄上面找到window–>Prefrences
  • 步驟二:按照下述步驟添加(注意點右側的Edit,才可以進行編輯、修改代碼)
    修改Eclipse中Web項目的jsp檔案表頭,自動加上basePath的原因及方法
    所需增加代碼如下:
<%  
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
           

最後點選Apply即可,下次再打開新的jsp檔案時上面就自動有這句話了。

以上部分借鑒部落格:

如何修改Eclipse中Web項目的jsp檔案表頭,自動加上basePath

https://blog.csdn.net/weixin_41996974/article/details/80717426

繼續閱讀