java Wicket:相对于绝对 URL 或获取基本 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2619833/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:02:56  来源:igfitidea点击:

Wicket: Relative to absolute URL or get base URL

javaurlwicket

提问by Gilean

If I have a relative path to a static asset (flash/blah.swf), what is the best way to programmatically convert this to an absolute URL (http://localhost/app/flash/blah.swf)? Or what is the best way to get the base URL of the Wicket application? I've tried using RequestUtils.toAbsolutePath but it doesn't seem to work reliably and is frequently throwing exceptions. This needs to work on all servers the app is deployed to.

如果我有静态资产 (flash/blah.swf) 的相对路径,以编程方式将其转换为绝对 URL ( http://localhost/app/flash/blah.swf)的最佳方法是什么?或者获取 Wicket 应用程序基本 URL 的最佳方法是什么?我试过使用 RequestUtils.toAbsolutePath 但它似乎不能可靠地工作并且经常抛出异常。这需要在应用程序部署到的所有服务器上工作。

采纳答案by Richard Noble

RequestUtils.toAbsolutePath(RequestCycle.get().getRequest().
  getRelativePathPrefixToWicketHandler());

worked for me.

对我来说有效。

回答by Manuel Manhart

For Wicket 6 it is

对于 Wicket 6,它是

String absoluteUrl = RequestCycle.get().getUrlRenderer().renderFullUrl(Url.parse("my-relative-url.html"));

回答by Duncan McGregor

For Wicket 1.5 there is information here

对于 Wicket 1.5,这里有信息

回答by Don Roby

org.apache.wicket.protocol.http.servlet.ServletWebRequesthas a method getRelativePathPrefixToContextRoot()(actually defined as abstract in a superclass).

org.apache.wicket.protocol.http.servlet.ServletWebRequest有一个方法getRelativePathPrefixToContextRoot()(实际上在超类中定义为抽象)。

A standard idiom for using it is

使用它的标准习语是

RequestCycle.get().getRequest().getRelativePathPrefixToContextRoot() + location;

回答by Gilean

I ended up using something like this after adding a base_urlproperty to my MyApplicationclass that extends the wicket application.

在向base_url我的MyApplication类添加一个扩展检票口应用程序的属性后,我最终使用了这样的东西。

MyApplication app = (MyApplication)getApplication();
String appBaseUrl = app.getBaseUrl(); 
if (StringUtils.isEmpty(appBaseUrl)) { 
    appBaseUrl = RequestUtils.toAbsolutePath(urlFor(app.getHomePage(), new PageParameters()).toString());
    app.setBaseUrl(appBaseUrl); 
} 

// Add base URL to <script wicket:id="base_url"></script> to use with Flash
add(new Label("base_url", "base_url = \"" + appBaseUrl + "\";").setEscapeModelStrings(false));