[精讚] [會員登入]
1029

【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 Workbench】如何透過TCP/IP進行SSL連線到遠端MySQL資料庫 通常MySQL伺服器都不是只提供某人連線,而是多人都可以連線進來使用,這時就不能繼續使用localhost的那種寫法

【Wicket】[CSP] Content-Security-Policy & Content-Security-Policy-Report-Only Wicket 預設開啟的,所以如果要加載外部資源甚至是同源資源都會被擋下。

【Python3】[Django] (Windows / Liunx) 如何從零開始創建一個網站 除了最基本的運作以外,還小小的加上了如何自導向特定目錄。

【Linux】[CentOS 8] How to update sudo instruction The last time I wanted to update the sudo command should be 2017, right? It's 2021 in a blink of an

【Maven】如何創建一個簡單可執行的JAR檔 滿重要的一個小功能,畢竟有時候是要傳遞的是JAR檔,而不是直接透過Maven直接部屬之類的

【Wickct】(縮短網址) 如何將網頁掛載到特定路徑下 Wildfly的預設網址又臭又長又不好記,而且會被看到後端的目錄路徑安排,當然能藏就盡量藏起來啦

隨機好文

高捷少女:耐耐的新年驚喜④ 「雖然夫人一直有在保養身體,可是年紀畢竟不小了,醫生曾經說過,不管是不是假性陣痛,一旦夫人有感覺了便立刻送到醫院。」管家爺爺說:「我們已經打電話給附近的大型醫院,救護車很快便會來到這裡。」「去看伯母吧

婕兒──她的青春① 「各位乘客,本列車即將抵達拉里奧哈自治區,並在此地停留三天兩夜,後天的中午十二點將搭乘班機返回臺灣,感謝各位乘客對本次旅程的配合。」火車上的廣播器朗誦道。「時間過得真快呢,這次的歐洲之旅就這樣結束了,

【日翻中歌詞】LOSER 一如往常的孤身一人 早就已經被折磨殆盡 明明就已經無處可去 卻作著白日夢 晚安 無論何時都是這個樣子 對懵懂夜晚早感到噁心

【中翻英歌詞】(二創歌)密匿的四個季節 Rap Battles (Gap Battles!) of Gensokyo. 幻想鄉饒舌(隙間!)大戰 Secret God, Okina Matara versus Youkai Sage, Yu

小穹‧動畫化‧體驗記② 「想不到真的有這麼一天!」婕兒高興地說:「我們要動畫化了耶!我覺得今天晚上我一定會睡不著。」 「就是說啊,如果這個消息傳了出去,粉絲們一定也會很高興的。」耐耐喝了一口湯。