2010-04-12 56 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