开发者

GWT replacement for java.net.URL

开发者 https://www.devze.com 2023-01-28 02:25 出处:网络
I have to replace the protocol part of a already existing url in GWT. The java.net package has a class which was build for exactly that purpose: URL. Sadly GWT does not emulate the java.net package.

I have to replace the protocol part of a already existing url in GWT. The java.net package has a class which was build for exactly that purpose: URL. Sadly GWT does not emulate the java.net package.

How can I reassemble a url in GWT without creating my own parser? (I know about UrlBuilder, but UrlBuilder won't take an existing URL)

Example: I have a url in a string "http://myserver.开发者_开发技巧com/somepath/file.html?param" and I want to replace the protocol part with "https".


public void onModuleLoad() {
    Button btn = new Button("change protocol");
    btn.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            UrlBuilder builder = Window.Location.createUrlBuilder().setProtocol("https");
            Window.Location.replace(builder.buildString());
        }
    });
    RootPanel.get().add(btn);
}


It's ugly, but you can always create an anchor element and extract the parts from there.

AnchorElement a = Document.get().createAnchorElement();
a.setHref("http://test.com/somerandompath");
Window.alert(a.getPropertyString("protocol") + " " + a.getPropertyString("host")) + " " a.getPropertyString("pathname"));

a.removeFromParent();


Does Window.Location help you at all? You can read out the URL there, mod it and .assign() it back.

0

精彩评论

暂无评论...
验证码 换一张
取 消