[精讚] [會員登入]
466

【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?! 解題時所意外研究出來的神奇寫法,懂了原理之後要自行改寫成四捨五入或無條件進位應該就簡單了吧!

【C++】使用SFML製作讓方塊落下的畫面 從開啟新視窗延伸過來的應用(?),配合上一篇所使用到開啟一個可渲染視窗的那堆程式碼的延伸。

【Maven】如何創建一個簡單可部屬的WAR檔 滿重要的一個大功能,在使用JAVA網頁伺服器的時候一定會需要這個WAR檔來進行部屬

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

【C++】如何解決TLE,換句話說便是加速cin, cout的執行速度 [ZERO JUDGE](UVa) a159: 11743 - Credit Check 題目練習和副程式練習

【Discord bot】(ERROR)await self.bot.wait_for() 大坑 今天真的是採到大坑,只找到解決方法,具體原因不明

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

希萌創意預計在今年7月繼東津萌米之後再次推出新遊戲--食用性少女! 今天來介紹希萌創意的心企劃案,來讓大家知道這個消息!讓大家的錢包君一起來減肥吧!Ψ(☆w☆)

高捷少女:美麗島的守護者③ 小雅閉上眼睛,思索在高捷發生的點點滴滴。她心意已決,在高捷的日子的確也有快樂的部分,不過她相信換個方向是更好的決定。有關高捷的所有美好回憶,小雅決定保留在心裡就好,繼續在高捷工作只會讓自己更痛苦而已,

【歌評】蓮台野夜行 - 幻視之夜 ~ Ghostly Eyes 若有什麼是在聽到boss曲前的鋪襯,那一定就是每個系列的道中曲

【英翻中歌詞】(二創歌)妖精大戰爭 ~Fairy Wars 妖精大戦争 ~Fairy Wars Star light… star bright… 星光閃耀...星輝熠熠... We’re the three brightest stars in the sky tonight! 我們是今晚

【札記】在發文上遇到的問題 如果遇到問題,是選擇丟在一旁還是將其解決,又或者是看情況?