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

时间:2010-04-12 04:07:53

标签: java url wicket

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

5 个答案:

答案 0 :(得分:13)

对于Wicket 6来说,它是

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

答案 1 :(得分:8)

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

为我工作。

答案 2 :(得分:1)

对于Wicket 1.5,有信息here

答案 3 :(得分:0)

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

使用它的标准习惯用语是

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

答案 4 :(得分:0)

我将base_url属性添加到扩展wicket应用程序的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)); 
相关问题