网址过滤器中的网址重写

时间:2013-10-08 11:38:14

标签: servlets servlet-filters requestdispatcher

在我的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

我做错了什么?

1 个答案:

答案 0 :(得分:0)

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

  

...

     

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

     

...

相关问题