[精讚] [會員登入]
1134

【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

你可能感興趣的文章

【MySQL】每個類別中取前三高 一個由多層次所組成的SELECT述句

【Discord bot】[botton]按鈕的使用、響應和關閉 Discord的botton通常都要和View配合使用。

【C++】一些好用的C++小功能 —— 壹 不太重要,但是可以增加程式質量以及可以讓程式看起來好像很厲害

【Linux】網路測速 network speed test 在沒有 GUI 的狀態之下,該如何進行簡單的網路測速呢?

【Maven】如何夾帶檔案至Jar內以及其讀取方式 想要讀取一個外部的文字文件?Maven是你的好幫手

【C++】class練習 — 檢測該字串是否為迴圈 第一次的C++結構式寫法,雖然以後應該會見怪不怪,但畢竟是第一次所以還是想保存下來

隨機好文

【手遊介紹】小品手遊─寶箱是我的!(SUMMONER'S GREED) 輕鬆無腦的塔防遊戲,殺時間本小編推薦的遊戲之一

[活動] 2017年4/1雲空幻想愚人節活動彩蛋&攻略 (紀念性質) 雲空幻想2017年的愚人節活動介紹同時也是本小編的第一篇網路文章(*^ω^)♪

高捷少女:地下城的探險少女① 婕兒心中一奇,便走上前看著仔細。那塊凹進去的地方中心大約三公分厚,越往邊緣就越淺,圓型直徑十五公分。婕兒拿出銅盤對比一下,發現兩者大小竟然一致,銅盤似乎能夠完整的嵌進去。     婕兒看著凹槽,心中

【歌評】蓮台野夜行 - 幻想的永遠祭 蓮台野的探險雖然結束了,但是,誰知道是不是一段新的探險的序曲呢?

艾米莉亞和高捷戀旅② 七點三十五分了。 「快到粉絲團!快!」小穹慌張地說,婕兒、耐耐也湊向手機。雖然她們沒有報名,不過也希望亦晨參賽,不免著急起來。 「亦晨?妳的報名序號是多少?」艾米快速滑動手機。合格的參賽者姓名和序號都