[精讚] [會員登入]
1138

【Wicket】[URI]使用Java取得目前網址

這是個還滿常用的功能,可惜 Wicket 沒有內建函式可以快速取得我們要的部分,只能依靠我們自己後期的字串剖析

分享此文連結 //n.sfs.tw/15711

分享連結 【Wicket】[URI]使用Java取得目前網址@小編過路君子
(文章歡迎轉載,務必尊重版權註明連結來源)
2022-04-02 17:34:11 最後編修
2022-03-26 10:35:24 By 過路君子
 

哈囉大家好,這裡是沒有提供就自己處理的小編過路君子

大概查了一下,沒有像 javascript 的 location 那樣的函式可以快速取得網址的各個部分,只查到取得完整的網址的方式。

 

 

這篇主要是來記錄如何取得網址的各部分,其實只要取的完整地網址,其他部分也呼之欲出了。

只是小編不想以後每次要取得當前網址都還要重寫一次相同的東西,所以特別用此篇記錄下來,方便未來的小編過來抄作業。

另外一點是,像是 Url 這個 class 的資訊竟然在 Wicket 9.x API 裡面找不到,只能透過間接的方式找到。

 

小編是不知道如 IntelliJ IDEA 這類方便好用的軟體能不能自動補上 import 資訊,但是小編本身是使用 Vim 來寫程式,連自動補齊都沒有了,更何況這種高級功能。

所以極度仰賴官方的說明文檔和小編自己的筆記,所以這類的東西不想找第二次,那乾脆直接寫下來好了。

 

那假設我們現在在 http://192.168.88.128:8080/index/login/ 下。

那我們可以透過以下的方式來快速取得當前網址。

import org.apache.wicket.request.Url;
import org.apache.wicket.request.cycle.RequestCycle;

public class index extends WebPage
{
    public index()
    {
        Url url = RequestCycle.get().getRequest().getUrl();
        String href = RequestCycle.get().getUrlRenderer().renderFullUrl(url);
    }
}

現在 href 就是我們所在的完整網址囉~

(所有圖片點擊都可以放大、變高清)

 

那一條一條列出感覺很普通,所以小編決定將其寫成一個 class,而且用法就跟 javascript 的 location 一模一樣,這樣就簡單多了,沒有重新適應的煩惱。

import org.apache.wicket.request.Url;
import org.apache.wicket.request.cycle.RequestCycle;

private class Location
{
    private String href;
    private String host;
    private String port;
    private String search;
    private String hostname;
    private String pathname;
    private String protocol;

    public Location()
    {
        Url url = RequestCycle.get().getRequest().getUrl();
        this.href = RequestCycle.get().getUrlRenderer().renderFullUrl(url);

        int theFirstColon = this.href.indexOf(":");
        int haveQuestionMark = this.href.indexOf("?");
        int pathStart = this.href.indexOf("/", theFirstColon+3);

        this.protocol = this.href.substring(0, theFirstColon + 1);
        this.host = this.href.substring(theFirstColon + 4, pathStart);

        int theSecondColon = this.host.indexOf(":");

        this.hostname = (theSecondColon == -1)? this.host : this.host.substring(0, theSecondColon);
        this.port = (theSecondColon == -1)? "80" : this.host.substring(theSecondColon+1);
        this.pathname = (haveQuestionMark == -1)? this.href.substring(pathStart) : this.href.substring(pathStart, haveQuestionMark);
        this.search = (haveQuestionMark == -1)? "" : this.href.substring(haveQuestionMark);
    }

    public String href() {return this.href;}
    public String host() {return this.host;}
    public String port() {return this.port;}
    public String search() {return this.search;}
    public String hostname() {return this.hostname;}
    public String pathname() {return this.pathname;}
    public String protocol() {return this.protocol;}
}

基本上就跟 javascript 的 location 一模一樣,除了 hash 以外其他都能獲取。

假設我們現在的網址為:https://example.info/index/login?u=user

函式 效果
href() https://example.info/index/login?u=user
host() example.info
port() 80
search() ?u=user
hostname() example.info
pathname() /index/login
protocol() https:

那使用方式也很簡單,首先我們先在所要獲取網址頁面對應的 java 添加上方整個的程式碼。

然後實體化這個 class 出來,就可以直接呼叫對應的函式來取得我們所需的資訊,然後返回值的類型都是 String,跟 javascript 的 location 一樣。

import org.apache.wicket.request.Url;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.request.cycle.RequestCycle;

public class index extends WebPage
{
    public index()
    {
        Location location = new Location();

        location.href()    //取得完整網址
        location.port()    //取得當前的埠
    }

    private class Location
    {
        ...
    }
}

 

 

 

後記

其實原本這篇文章只打算紀錄如何獲取完整網址的方式,結果後來想想,雖然不一定會用到,但是未來有個完整的作業可以抄不香嗎。

所以就試著來實現 javascript 的 location 效果,結果還不錯。

END

你可能感興趣的文章

【Docker&Wildfly】(bitnami/wildfly)如何從零開始創建網頁伺服器 使用他人的 docker image 來架設我們的 wildfly web server

【NodeMCU / ESP8266】[Arduino]測量溫溼度並上傳至ThinkSpeak 使用簡單的溫度計來測量溫濕度並上傳至第三方網站,這樣他人只要有瀏覽器就能看到我們的數據了

【Firefox \ Maven】[Headless](Linux) 如何使用Maven打包並驅動Firefox 有時候我們需要取得渲染後的網頁,當然是直接呼叫瀏覽器出來用啦

【CoreProtect】自行升級至 Minecraft 1.21.x、1.22.x 或更高版本 不知道為什麼,官方竟然無預警停止更新,我們只好自行救濟

【Stable Diffusion web UI】[AI 作畫](Linux)無 NVIDIA 顯卡之伺服器運行測試 凡事就是要試試看,才知道最後的結果

【C】(%c, %d)解決讀取字元時的緩衝區殘留 不解決就會莫名其妙地冒出一些莫名其妙的東西

隨機好文

高捷少女:小穹與果仁巧克力㊥ 艾咪臉上泛起笑容說:「妳知道嗎?就跟花語一樣,每一種巧克力也都有各自的涵義:薄荷巧克力代表初戀;卡通巧克力代表天真爛漫;而果仁巧克力代表的是窩心,還有想陪伴對方的心情,這在德國是女生之間在慶生時會彼此

高捷少女:購票大作戰(終) 完了。 這下真的完了! 艾米腦內所有的思考中樞,同時發出了淒厲的吶喊。完蛋啦!這下真的被揭穿了啦!! 「那……那是因為……」艾米結結巴巴的說:「因為……

高捷少女:美麗島的守護者① 婕兒跟耐耐嚇得臉色發白。「耐耐,小穹有這方面的愛好喔?」婕兒小聲地說。耐耐默默的拿出筆記本,臉上浮現一層微笑:「這麼大的新聞,要趕快記下來!」

高捷少女:美麗島的守護者② 這是在亦晨離開前的下午拍的,當時亦晨在美麗島跟小穹艾米等人在美麗島散步,為離開前補充一些回憶,這時剛好經過的小雅被艾米抓過來,做為同樣是高捷新人的她倆一同拍了一張紀念照。說起來她跟亦晨並不太熟,不過也

【歌評】蓮台野夜行 - 魔術師梅莉(魔術師メリー)  對於同一首歌每一個人都有不同的見解,看看別人對於這一首歌的看法,說不定就可以聽出這首歌想要表達的事情!