【问题标题】:Url rewriting in web filter网页过滤器中的 URL 重写
【发布时间】:2013-10-08 11:38:14
【问题描述】:

在我的 JSF 应用程序中,我想实现一个 Web 过滤器来更改所用设备功能中请求的视图(我使用 spring-mobile 设备解析器)。

我的过滤器中有这个:

    String requestURI = request.getRequestURI();

    Device device = DeviceUtils.getCurrentDevice(request);

    if (!requestURI.contains("/mobile") && device.isMobile()) {
        String newUri = requestURI.replace("/contextroot/faces/html/", "/contextroot/faces/html/mobile/");
        request.getRequestDispatcher(newUri).forward(request, response);
    }
    else {
        filterChain.doFilter(request, response);
    }

但我得到一个例外

/contextroot/faces/html/mobile/consult/consult.xhtml Not Found in ExternalContext as a Resource

我做错了什么?

【问题讨论】:

  • 它适用于 requestURI.replace("/contextroot/faces/html/", "/faces/html/mobile/");

标签: servlets servlet-filters requestdispatcher


【解决方案1】:

HttpServletRequest#getRequestDispatcher() 采用相对于上下文根的路径,因此您不应包含上下文根本身的路径。这在javadoc(强调我的)中有明确规定:

...

指定的路径名​​可以是相对的,尽管它不能扩展到当前 servlet 上下文之外。 如果路径以“/”开头,则将其解释为相对于当前上下文根。如果 servlet 容器无法返回 RequestDispatcher,则此方法返回 null

...

【讨论】:

    猜你喜欢
    • 2013-03-16
    • 2011-07-25
    • 1970-01-01
    • 2013-02-26
    • 2012-07-11
    • 2023-03-14
    • 2015-01-05
    • 2011-05-28
    • 1970-01-01
    相关资源
    最近更新 更多