【问题标题】:How to get the real request URL in Struts with Tiles?如何在 Struts with Tiles 中获取真实的请求 URL?
【发布时间】:2008-09-25 08:28:19
【问题描述】:

当您将 Tiles 与 Struts 一起使用时...

request.getRequestURL()

...你得到的 URL 例如/WEB-INF/jsp/layout/newLayout.jsp 而不是用户输入/点击的真实 URL,类似于 /context/action.do

在较新的 Struts 版本 1.3.x 及更高版本中,您可以使用 solution mentioned on javaranch 并使用请求属性 ORIGINAL_URI_KEY 获取真实 URL。

但是如何在 Struts 1.2.x 中做到这一点?

【问题讨论】:

    标签: java servlets struts tiles


    【解决方案1】:

    我使用这个,它也适用于 Spring:

    <% out.println(request.getAttribute("javax.servlet.forward.request_uri")); %>
    

    如果您还需要查询字符串(matchew 提供):

    <% out.println(request.getAttribute("javax.servlet.forward.query_string")); %>
    

    【讨论】:

      【解决方案2】:

      当您从 view/jsp/tiles 层查询 request.getRequestURL() 时,它已经是另一个重写的请求。

      正如 Mwanji Ezana 所提到的,最合适的方法是将其保存到动作执行阶段的单独属性中。您可能希望借助 Struts2 中的拦截器自动执行此过程。

      【讨论】:

        【解决方案3】:

        我不知道 Struts 1.2.x 是否有类似的 Globals 常量,但您至少可以通过两种方式创建自己的:

        • 在 Action 中获取原始请求 URL 并将其设置在请求中,并从 JSP 中调用它
        • 使用 Servlet 过滤器来做同样的事情

        【讨论】:

          【解决方案4】:

          这适用于 Struts 1.2

          private String getOriginalUri(HttpServletRequest request) {
              String targetUrl = request.getServletPath();
              if (request.getQueryString() != null) {
                  targetUrl += "?" + request.getQueryString();
              }
              return targetUrl;
          }
          

          【讨论】:

          • 不,在OP给出的情况下,它肯定不会。
          【解决方案5】:

          您只需要在您的操作中执行此操作:

              request.getAttribute("javax.servlet.forward.request_uri")
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-02-05
            • 1970-01-01
            相关资源
            最近更新 更多