Java Wicket:相对于绝对URL或获取基本URL
如果我有一个指向静态资产(flash/blah.swf)的相对路径,那么以编程方式将其转换为绝对URL()的最佳方式是什么?或者,获取Wicket应用程序的基本URL的最佳方法是什么?我尝试过使用RequestUtils.ToAbsolutionPath,但它似乎工作不可靠,经常抛出异常。这需要在应用程序部署到的所有服务器上运行。Java Wicket:相对于绝对URL或获取基本URL,java,url,wicket,Java,Url,Wicket,如果我有一个指向静态资产(flash/blah.swf)的相对路径,那么以编程方式将其转换为绝对URL()的最佳方式是什么?或者,获取Wicket应用程序的基本URL的最佳方法是什么?我尝试过使用RequestUtils.ToAbsolutionPath,但它似乎工作不可靠,经常抛出异常。这需要在应用程序部署到的所有服务器上运行。org.apache.wicket.protocol.http.servlet.ServletWebRequest有一个方法getRelativePathPrefixT
org.apache.wicket.protocol.http.servlet.ServletWebRequest
有一个方法getRelativePathPrefixToContextRoot()
(实际上在超类中定义为抽象)
使用它的标准习语是
RequestCycle.get().getRequest().getRelativePathPrefixToContextRoot() + location;
在扩展wicket应用程序的
MyApplication
类中添加了base\uURL
属性之后,我最终使用了类似的东西
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));
MyApplication-app=(MyApplication)getApplication();
字符串appBaseUrl=app.getBaseUrl();
if(StringUtils.isEmpty(appBaseUrl)){
appBaseUrl=RequestUtils.ToAbsolutionPath(urlFor(app.getHomePage(),new PageParameters()).toString());
app.setBaseUrl(appBaseUrl);
}
//添加要与Flash一起使用的基本URL
添加(新标签(“基本url”,“基本url=\”“+appBaseUrl+“\”;”))。setEscapeModelString(false));
为我工作。对于Wicket 1.5,有关于Wicket 6的信息
String absoluteUrl = RequestCycle.get().getUrlRenderer().renderFullUrl(Url.parse("my-relative-url.html"));
请注意,这在Wicket 1.5中不起作用,它没有单个arg
toAbsolutionPath
,基本URL是String baseUrl=RequestCycle.get().getUrlRenderer().getBaseUrl().toString()
String absoluteUrl = RequestCycle.get().getUrlRenderer().renderFullUrl(Url.parse("my-relative-url.html"));