3

在我使用 PrimeFaces 3.4.2 的 JSF2.1 Web 应用程序中,我添加了一个新网页,其中仅包含一个使用renderKitId="PRIMEFACES_MOBILE"(PFM 0.9.3) 的视图。这个想法是过滤器将来自移动设备的请求重定向到此页面。不幸的是,这个过滤器完全破坏了某些移动设备上移动页面的 CSS(是的,并非所有设备都受到影响!)。当过滤器存在时,重定向呼叫和直接呼叫在受影响的设备上都会中断;当过滤器关闭时,一切正常。

这里是网络过滤器:

public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain)
        throws IOException, ServletException {

    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse resp = (HttpServletResponse) response;

    boolean isMobile = isMobileDevice(req.getHeader("user-agent"));  // utility function

    if (!req.getRequestURI().contains("/mobile/") && isMobile) {
        resp.sendRedirect(req.getContextPath() + "/faces/mobile/index.xhtml");
    } else {
        chain.doFilter(request, response);
    }    
}

过滤器在 web.xml 中没有任何映射。只有注释@WebFilter("/*")存在。当注释内的路径被伪造时,一切正常。

xhtml 页面...相当简单:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui"
      xmlns:pm="http://primefaces.org/mobile" >
    <h:head>
    </h:head>
    <h:body>
        <f:view renderKitId="PRIMEFACES_MOBILE">
            <pm:page title="Hello World">
                <pm:view id="main">
                    <pm:header title="Header" />
                </pm:view>
            </pm:page>        
        </f:view>    
    </h:body>
</html>

此处有关受影响设备的其他信息。我没有关于如何调试的提示。我已经使用 Firebug 查看了生成的 html,但无法检测到工作的一个和另一个之间的任何区别。

4

1 回答 1

10

您需要让过滤器跳过对 CSS/JS/图像文件的 JSF 资源请求。

if (req.getRequestURI().startsWith(req.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) {
    chain.doFilter(request, response);
    return;
}
于 2013-01-22T20:10:24.637 回答