[精讚] [會員登入]
1117

【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

你可能感興趣的文章

【C】〔無條件捨去〕如何忽略float數據 直接儲存成int型態(數據100%不失真) a148: You Cannot Pass?! 解題時所意外研究出來的神奇寫法,懂了原理之後要自行改寫成四捨五入或無條件進位應該就簡單了吧!

【Wicket】[Header]如何讀取來自客戶端地檔頭和傳送自訂擋頭至客戶端 當需要設定檔頭或是讀取來自客戶端的檔頭時,這些程式碼就很好用

【JDA/discord bot】刪除事件或slash(斜槓)指令的reply訊息 如何正確的等待 Async 的結束,在進行接下來的刪除訊息動作

【JAVA】[java.lang.Class]如何不透過new來實體化class 在某些極端狀態下,我們沒有辦法直接將實體化後的 Object 傳入函數內,只能採用傳入 Class 在實體化成物件這種極端的方式來進行

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

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

隨機好文

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

【英翻中歌詞】(二創歌)東方妖妖夢-人形裁判 ~玩弄人偶的少女 人形裁判 ~ 人の形弄びし少女 很久很久以前,在一個神奇的異地,住著一位少女,十分惹人憐愛 她的皮膚就像陶瓷一樣潔白,眼睛就像藍色寶珠般明亮

【數學】徐氏數學簡明講義(三) 第二章 直線與園 P2.1-15 Q12 12.已知聯立方程式恰有一解(x,y) = (1,2),   則的解為(x,y) = ? 解:

【小說EPUB下載】﹝持續更新﹞高捷少女二創小說全 網頁上的無名小說EPUB化了?!

【英翻中歌詞】(二創歌)佐渡的二岩 Outside, inside, inside, outside 由外而內,由內而外 Find the line and watch me