【问题标题】:Wicket: Relative to absolute URL or get base URLWicket:相对于绝对 URL 或获取基本 URL
【发布时间】:2010-04-12 04:07:53
【问题描述】:

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

【问题讨论】:

    标签: java url wicket


    【解决方案1】:

    Wicket 6 是

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

    【讨论】:

    • 基本网址是String baseUrl = RequestCycle.get().getUrlRenderer().getBaseUrl().toString()
    【解决方案2】:
    RequestUtils.toAbsolutePath(RequestCycle.get().getRequest().
      getRelativePathPrefixToWicketHandler());
    

    为我工作。

    【讨论】:

    • 请注意,这在 Wicket 1.5 中不起作用,它没有单个参数 toAbsolutePath
    【解决方案3】:

    Wicket 1.5 有信息here

    【讨论】:

      【解决方案4】:

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

      使用它的标准习语是

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

      【讨论】:

        【解决方案5】:

        在将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)); 
        

        【讨论】:

          猜你喜欢
          • 2011-02-02
          • 2011-09-09
          • 2012-08-28
          • 2010-09-12
          • 2011-02-26
          • 1970-01-01
          • 2010-10-03
          • 2011-01-15
          相关资源
          最近更新 更多