2010-04-12 69 views
13

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

回答

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

为我工作。

+3

请注意,这并不在检票1.5,它没有单ARG工作'toAbsolutePath' – 2012-01-18 12:11:18

0

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

标准的成语使用它是

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

最后我用这样的添加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)); 
1

检票1.5有信息here

12

检票六是

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

这适用于检票口1.5也是 – 1ac0 2014-01-02 15:19:32

+3

而基本URL是'String baseUrl = RequestCycle.get()。getUrlRenderer()。getBaseUrl()。toString()' – 2014-10-22 20:53:23