Login page does not display stylesheet after filter

'working on a JSF 2 project. I defined my login.xhtml page as the login page in web.xml

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

And I also have a filter to check if the user is registered

  @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); } } .... .... } 

My css files were pointed in the following style:

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

Everything worked fine until I changed the css link style to the JSF 2 resource handler ( http://www.mkyong.com/jsf2/resources-library-in-jsf-2-0/ ). I copied all my css files to the resources folder and gave the library name and version number. So now I refer to css as follows:

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

After the change, the login.xhtml file no longer displays the stylesheet. I have a welcome.xhtml page right after the login.xhtml page, which has an almost identical structure, with the exception of the main content, but this page looks great. I updated login.xhtml but it does not display. But as soon as I log in, go to the next page, then go back to login.xhtml and then refresh, the style will be displayed. Also, if I remove loginCheckFilter, login.xhtml will display a stylesheet. So, if someone is faced with a similar situation and knows how to resolve it? Thanks!

+4
source share
1 answer
 urlPatterns={"/*"} 

Your filter also blocks JSF resource requests.

You need to rewrite your filter so that it resolves JSF resource requests.

 @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); } } 
+5
source

All Articles