开发者

Wicket: Relative to absolute URL or get base URL

开发者 https://www.devze.com 2022-12-26 17:50 出处:网络
If I have a relative path to a static asset (flash/blah.swf), what is the best way to program开发者_StackOverflow中文版matically convert this to an absolute URL (http://localhost/app/flash/blah.swf)?

If I have a relative path to a static asset (flash/blah.swf), what is the best way to program开发者_StackOverflow中文版matically 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.


For Wicket 6 it is

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


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

worked for me.


For Wicket 1.5 there is information here


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

A standard idiom for using it is

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


I ended up using something like this after adding a base_url property to my MyApplication class that extends the wicket application.

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)); 
0

精彩评论

暂无评论...
验证码 换一张
取 消