简体   繁体   English

Wicket:从浏览器获取URL

[英]Wicket: Get URL from browser

I need to retrieve URL from current Web page opened in Firefox by using Wicket. 我需要使用Wicket从Firefox中打开的当前网页检索URL。 Can somebody tell me how to do that? 有人可以告诉我该怎么做吗?

You need to query the underlying HTTPServletRequest : 您需要查询基础HTTPServletRequest

public class DummyPage extends WebPage{

    private String getRequestUrl(){
        // this is a wicket-specific request interface
        final Request request = getRequest();
        if(request instanceof WebRequest){
            final WebRequest wr = (WebRequest) request;
            // but this is the real thing
            final HttpServletRequest hsr = wr.getHttpServletRequest();
            String reqUrl = hsr.getRequestURL().toString();
            final String queryString = hsr.getQueryString();
            if(queryString != null){
                reqUrl += "?" + queryString;
            }
            return reqUrl;
        }
        return null;

    }

}

Reference: 参考:

To get the current page's url use the webrequest and UrlRenderer: 要获取当前页面的网址,请使用webrequest和UrlRenderer:

Url url = ((WebRequest)RequestCycle.get().getRequest()).getUrl();
String fullUrl = RequestCycle.get().getUrlRenderer().renderFullUrl(url);

The solution from Sean Patrick Floyd seems to be obsolete for wicket 1.5 对于wicket 1.5来说,Sean Patrick Floyd的解决方案似乎已经过时了

If using wicket 1.5 (or above I guess) here is the solution: 如果使用wicket 1.5(或以上我猜)这里是解决方案:

RequestCycle.get().getUrlRenderer().renderFullUrl(
    Url.parse(urlFor(MyPage.class,null).toString()));

Reference : 参考

Getting a url for display 获取显示的网址

This works. 这有效。 I'm using wicket 1.5; 我正在使用wicket 1.5;

new URL(RequestCycle.get().getUrlRenderer().renderFullUrl( Url.parse(urlFor(HomePage.class,null).toString()))). 新URL(RequestCycle.get()。getUrlRenderer()。renderFullUrl(Url.parse(urlFor(HomePage.class,null).toString())))。 getAuthority() ; getAuthority() ;

Example: http://example.com:80/a_long_path/ 示例: http//example.com80 / a_long_path /

getAuthproty() will return example.com:80 getAuthproty()将返回example.com:80

getHost() will return example.com. getHost()将返回example.com。

Depending on what exactly you want, this may not be possible. 根据您的具体需求,这可能无法实现。 There is a short guide here in the Wicket wiki , but it has some caveats, notably that it only returns a relative URL in versions of Wicket after 1.3. Wicket wiki中有一个简短的指南,但它有一些注意事项,特别是它只返回1.3之后的Wicket版本中的相对URL。 That said, the method used is 也就是说,使用的方法是

String url = urlFor("pageMapName", MyPage.class, new PageParameters("foo=bar"));

If you go with the wiki's alternate method — the one involving the form — be warned: getPage() is not part of Wicket's public API . 如果您使用wiki的替代方法 - 涉及表单的方法 - 请注意: getPage() 不是Wicket的公共API的一部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM