【问题标题】:Getting Path (context root) to the Application in Restlet在 Restlet 中获取应用程序的路径(上下文根)
【发布时间】:2010-04-29 15:48:45
【问题描述】:

我需要在 Restlet 资源类中获取应用程序根目录(它扩展了 ServerResource)。我的最终目标是尝试将完整的显式路径返回到另一个资源。

我目前正在使用getRequest().getResourceRef().getPath(),这几乎可以满足我的需求。这不会返回完整的 URL(如 http://example.com/app),它会返回给我 /resourceName。所以我遇到了两个问题,一个是它缺少架构(http 或 https 部分)和服务器名称,另一个是它没有返回应用程序安装到的位置。

因此,鉴于“http://dev.example.com/app_name/person”处的人员资源,我想找到一种方法找回“http://dev.example.com/app_name”。

我正在使用 Restlet 2.0 RC3 并将其部署到 GAE。

【问题讨论】:

    标签: restlet


    【解决方案1】:

    看起来getRequest().getRootRef().toString() 给了我想要的东西。我尝试使用 getRequest().getRootRef() 的方法调用组合(如 getPathgetRelativePart),但要么他们给了我不想要的东西,要么 null em>。

    【讨论】:

      【解决方案2】:

      只需从服务上下文中获取基本 url,然后将其与资源共享并在需要时添加资源路径。

      MyServlet.init():
      
      String contextPath = getServletContext().getContextPath();
      getApplication().getContext().getAttributes().put("contextPath", contextPath);
      
      MyResource:
      
      String contextPath = getContext().getAttributes().get("contextPath");
      

      【讨论】:

        【解决方案3】:

        request.getRootRef()request.getHostRef()?

        【讨论】:

          【解决方案4】:

          可以从 restlet 的应用程序访问 servlet 的上下文:

          org.restlet.Application app = org.restlet.Application.getCurrent();
          javax.servlet.ServletContext ctx = ((javax.servlet.ServletContext) app.getContext().getAttributes().get("org.restlet.ext.servlet.ServletContext"));
          String path = ctx.getResource("").toString();
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-11-19
            • 2016-04-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多