Web过滤器会破坏PrimeFaces的移动视图

时间:2013-01-22 19:29:29

标签: jsf mobile primefaces servlet-filters

在使用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>

有关受影响设备的其他信息here。 我没有提示如何调试它。我已经使用Firebug查看了生成的html,但是无法检测到工作的html之间的任何差异。

1 个答案:

答案 0 :(得分:10)

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

if (req.getRequestURI().startsWith(req.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) {
    chain.doFilter(request, response);
    return;
}