How to redirect on JSF using PrimeFaces?

jsf, primefaces, servlet-filters

Solution

I often use this way to redirect it directly from Java Bean: `FacesContext.getCurrentInstance().getExternalContext().redirect(Url);`

Problem

I'm trying to do a basic security system on JSF where if the user is not logged in and try to access a restricted page he will be redirected to login.xhtml. This is done in a servlet filter. My problem is that when using resp.sendRedirect("login.xhtml"); the login page loses all resources, css, scripts, etc. So the page is displayed without any style. web.xml ``` <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <session-config> <session-timeout>30</session-timeout> </session-config> <welcome-file-list> <welcome-file>index.xhtml</welcome-file> </welcome-file-list> <filter> <filter-name>restrict</filter-name> <filter-class>br.com.jetcar.filter.RestrictionFilter</filter-class> </filter> <filter-mapping> <filter-name>restrict</filter-name> <url-pattern>*.xhtml</url-pattern> <servlet-name>Faces Servlet</servlet-name> </filter-mapping> </web-app> ``` template.xhtml ``` <!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:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.prime.com.tr/ui" xmlns:f="http://java.sun.com/jsf/core"> <h:head> <h:outputScript library="primefaces" name="jquery/jquery.js"/> <h:outputStylesheet library="css" name="template.css" /> <h:outputScript name="clock.js" library="js"></h:outputScript> <title><ui:insert name="title">Bem-Vindo</ui:insert></title> </h:head> <body> <p:growl id="messages"/> <div id="header"> <ui:insert name="header"> <div id="headerTitle">JetCar</div> <div id="clock"></div> </ui:insert> </div> <div id="menu"> <ui:insert name="menu"> <ui:include src="menu.xhtml" /> </ui:insert> </div> <div id="content"> <ui:insert name="content"> </ui:insert> </div> <div id="footer"> <ui:insert name="footer"> <p>Desenvolvido por</p> <p>Rodrigo Cavalcante de Souza</p> <p>Jorge Luis</p> </ui:insert> </div> </body> </html> ``` login.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"> <ui:composition template="resources/template/template.xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui"> <ui:define name="title">Login</ui:define> <ui:define name="content"> </ui:define> </ui:composition> ``` The index.xhtml looks the sames as the login, since I'm still testing, what could be the problem? error from chrome console ``` GET http://localhost:8080/JetCar/javax.faces.resource/growl/assets/login.xhtml 404 (Not Found) login.xhtml:3GET http://localhost:8080/JetCar/javax.faces.resource/themes/sam/login.xhtml 404 (Not Found) login.xhtml:6GET http://localhost:8080/JetCar/javax.faces.resource/jquery/login.xhtml 404 (Not Found) login.xhtml:6GET http://localhost:8080/JetCar/javax.faces.resource/login.xhtml 404 (Not Found) login.xhtml:6GET http://localhost:8080/JetCar/javax.faces.resource/core/login.xhtml 404 (Not Found) login.xhtml:4GET http://localhost:8080/JetCar/javax.faces.resource/login.xhtml 404 (Not Found) login.xhtml:6GET http://localhost:8080/JetCar/javax.faces.resource/growl/login.xhtml 404 (Not Found) login.xhtml:10Uncaught ReferenceError: jQuery is not defined chrome-extension://ffdcfjdljhbehggjdkdioajnknjcpbjb/js/sbc_cookies_mon.js:1Uncaught TypeError: Cannot read property 'tagName' of null ``` I'm using primefaces 2.2.1, glassfish 3.1, JSF 2.0 EDIT: I just noticed that the resources use the .xhtml extension as well, so it was interfering with the filter. FIX:`if(req.getSession().getAttribute("func") == null && !pageRequested.contains("login.xhtml") && !pageRequested.contains("/javax.faces.resource")) resp.sendRedirect("login.xhtml");`

Original source