2013-02-26 4 views
4

필터 뒤에 로그인 페이지가 스타일 시트를 렌더링하지 않습니다. 나는 web.xml을JSF 2 프로젝트에서 작업중인

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

의 항목 페이지로 내 login.xhtml 페이지를 정의 그리고 나는 또한 사용자가 다음에 언급 된

@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 파일

에 기록되어 있는지 확인하기 위해 필터가 스타일 : 나는 JSF 2 리소스 처리기 ( http://www.mkyong.com/jsf2/resources-library-in-jsf-2-0/)에 CSS를 참조 스타일을 변경할 때까지

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

모든 것이 잘 작동합니다. 리소스 폴더 아래에있는 모든 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

+0

당신을 진심으로 환영합니다. – BalusC

+0

@BalusC 사랑해. 너는 착한 사람이야. – Foriger