【发布时间】:2010-04-12 04:07:53
【问题描述】:
如果我有静态资产的相对路径 (flash/blah.swf),以编程方式将其转换为绝对 URL (http://localhost/app/flash/blah.swf) 的最佳方法是什么?或者获取 Wicket 应用程序的基本 URL 的最佳方法是什么?我试过使用 RequestUtils.toAbsolutePath 但它似乎不能可靠地工作并且经常抛出异常。这需要在应用程序部署到的所有服务器上工作。
【问题讨论】:
如果我有静态资产的相对路径 (flash/blah.swf),以编程方式将其转换为绝对 URL (http://localhost/app/flash/blah.swf) 的最佳方法是什么?或者获取 Wicket 应用程序的基本 URL 的最佳方法是什么?我试过使用 RequestUtils.toAbsolutePath 但它似乎不能可靠地工作并且经常抛出异常。这需要在应用程序部署到的所有服务器上工作。
【问题讨论】:
Wicket 6 是
String absoluteUrl = RequestCycle.get().getUrlRenderer().renderFullUrl(Url.parse("my-relative-url.html"));
【讨论】:
String baseUrl = RequestCycle.get().getUrlRenderer().getBaseUrl().toString()
RequestUtils.toAbsolutePath(RequestCycle.get().getRequest().
getRelativePathPrefixToWicketHandler());
为我工作。
【讨论】:
toAbsolutePath
Wicket 1.5 有信息here
【讨论】:
org.apache.wicket.protocol.http.servlet.ServletWebRequest 有一个方法getRelativePathPrefixToContextRoot()(实际上定义为超类中的抽象)。
使用它的标准习语是
RequestCycle.get().getRequest().getRelativePathPrefixToContextRoot() + location;
【讨论】:
在将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));
【讨论】: