2013-02-26 46 views
4

正在处理JSF 2项目之后呈现样式表。我定义我的login.xhtml页面的入口页面在web.xml登录页面不会在过滤器

<welcome-file-list> 
     <welcome-file>login.xhtml</welcome-file> 
    </welcome-file-list> 

而且我也有一个过滤器来检查,如果用户登录

@WebFilter(filterName = "loginCheckFilter", urlPatterns={"/*"}) 
    public class LoginCheckFilter implements Filter 
    { 
     @Inject 
     private LoginStatus loginStatus; 

     public void do Filter(...) 
     { 
      try{ 
      HttpServletRequest req = (HttpServletRequest) request; 
      HttpServletResponse res = (HttpServletResponse) response; 

      String path = req.getRequestURI(); 
      if(StringUtils.isNotBlank(path) 
       && StringUtils.contains(path, ".xhtml") 
       && !StringUtils.endsWith(path, "login.xhtml")) 
      { 
        if(loginStatus == null 
         || !loginStatus.isLoggedIn()) 
        { 
          res.sendRedirect(req.getContextPath() + "/login.xhtml"); 
         } 
        else 
         { 
          chain.doFilter(request, response); 
         } 
       } 
       else 
       { 
        chain.doFilter(request, response); 
       } 
      }catch (Exception ex) 
      { 
        log.error(ex); 
       } 
      } 

     .... .... 
     } 

我的CSS文件在以下被引用风格:

<link href="css/styles.css" rel="stylesheet" type="text/css"/> 

一切工作顺利,直到我更改CSS引用样式到JSF 2资源处理程序(http://www.mkyong.com/jsf2/resources-library-in-jsf-2-0/)。我已将所有css文件复制到资源文件夹下,并提供了库名称和版本号。因此,现在我参考如下的css:

<h:outputStylesheet library="default" name="css/styles.css"/> 

更改后,login.xhtml不再呈现样式表。我在login.xhtml页面后面有一个welcome.xhtml页面,除了核心内容外,它的结构几乎完全相同,但是这个页面完全没问题。我刷新了login.xhtml,但它仍未呈现。但是一旦我登录后,进入下一页,然后返回到login.xhtml,然后刷新,风格将得到呈现。另外,如果我取消loginCheckFilter,login.xhtml将呈现样式表。那么如果有人遇到类似的情况,并知道如何解决它?谢谢!

回答

5
urlPatterns={"/*"} 

您的过滤器还阻止对JSF资源的请求。

您需要以允许JSF资源请求的方式重写过滤器。

@Override 
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {  
    HttpServletRequest request = (HttpServletRequest) req; 
    HttpServletResponse response = (HttpServletResponse) res; 
    String loginURL = request.getContextPath() + "/login.xhtml"; 

    boolean loggedIn = loginStatus != null && loginStatus.isLoggedIn(); 
    boolean loginRequest = request.getRequestURI().startsWith(loginURL); 
    boolean resourceRequest = request.getRequestURI().startsWith(request.getContextPath() + "/faces" + ResourceHandler.RESOURCE_IDENTIFIER); 

    if (loggedIn || loginRequest || resourceRequest)) { 
     if (!resourceRequest) { // Prevent restricted pages from being cached. 
      response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1. 
      response.setHeader("Pragma", "no-cache"); // HTTP 1.0. 
      response.setDateHeader("Expires", 0); // Proxies. 
     } 

     chain.doFilter(request, response); 
    } else { 
     response.sendRedirect(loginURL); 
    } 
} 
+0

它的工作原理。非常感谢! – chaoshangfei 2013-02-27 22:49:20

+0

不客气。 – BalusC 2013-02-27 23:41:30

+0

@BalusC我爱你,你是个好人。 – Foriger 2015-01-29 21:40:40