java Wicket:从浏览器获取 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3886853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wicket: Get URL from browser
提问by sonjafon
I need to retrieve URL from current Web page opened in Firefox by using Wicket. Can somebody tell me how to do that?
我需要使用 Wicket 从在 Firefox 中打开的当前网页中检索 URL。有人能告诉我怎么做吗?
回答by Sean Patrick Floyd
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:
参考:
- (Wicket) Component.getRequest()
- (Wicket) Request
- (Wicket) WebRequest
- (Servlet API) HTTPServletRequest
- (检票口)Component.getRequest()
- (检票口)请求
- (检票口)WebRequest
- (Servlet API) HTTPServletRequest
回答by spuas
The solution from Sean Patrick Floyd seems to be obsolete for wicket 1.5
Sean Patrick Floyd 的解决方案对于 wicket 1.5 似乎已经过时
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:
参考:
回答by vijeshme
To get the current page's url use the webrequest and UrlRenderer:
要获取当前页面的 url,请使用 webrequest 和 UrlRenderer:
Url url = ((WebRequest)RequestCycle.get().getRequest()).getUrl();
String fullUrl = RequestCycle.get().getUrlRenderer().renderFullUrl(url);
回答by Michael Tarimo
This works. I'm using wicket 1.5;
这有效。我正在使用检票口 1.5;
new URL(RequestCycle.get().getUrlRenderer().renderFullUrl( Url.parse(urlFor(HomePage.class,null).toString()))).getAuthority();
新 URL(RequestCycle.get().getUrlRenderer().renderFullUrl(Url.parse(urlFor(HomePage.class,null).toString())))。getAuthority();
Example: http://example.com:80/a_long_path/
示例:http: //example.com: 80/a_long_path/
getAuthproty() will return example.com:80
getAuthproty() 将返回 example.com:80
getHost() will return example.com.
getHost() 将返回 example.com。
回答by Pops
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
取决于您到底想要什么,这可能是不可能的。Wicket wiki 中有一个简短的指南,但它有一些警告,特别是它仅在 1.3 之后的 Wicket 版本中返回相对 URL。也就是说,使用的方法是
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.
如果你去维基的另一种方法-一个涉及形式-被警告:getPage()
是不是Wicket的公共API的一部分。