Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wicket: Get URL from browser

Tags:

java

wicket

I need to retrieve URL from current Web page opened in Firefox by using Wicket. Can somebody tell me how to do that?

like image 700
sonjafon Avatar asked Oct 08 '10 00:10

sonjafon


4 Answers

You need to query the underlying 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:

  • (Wicket) Component.getRequest()
  • (Wicket) Request
  • (Wicket) WebRequest
  • (Servlet API) HTTPServletRequest
like image 104
Sean Patrick Floyd Avatar answered Nov 03 '22 10:11

Sean Patrick Floyd


To get the current page's url use the webrequest and UrlRenderer:

Url url = ((WebRequest)RequestCycle.get().getRequest()).getUrl();
String fullUrl = RequestCycle.get().getUrlRenderer().renderFullUrl(url);
like image 31
vijeshme Avatar answered Nov 03 '22 08:11

vijeshme


The solution from Sean Patrick Floyd seems to be obsolete for wicket 1.5

If using wicket 1.5 (or above I guess) here is the solution:

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

Reference:

Getting a url for display

like image 5
spuas Avatar answered Nov 03 '22 09:11

spuas


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. 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.

like image 1
Pops Avatar answered Nov 03 '22 08:11

Pops