如何使用PrimeFaces在JSF上重定向?

如何使用PrimeFaces在JSF上重定向?,jsf,primefaces,servlet-filters,Jsf,Primefaces,Servlet Filters,我试图在JSF上建立一个基本的安全系统,如果用户没有登录并尝试访问一个受限页面,他将被重定向到login.xhtml。这是在servlet过滤器中完成的 我的问题是,当使用resp.sendRedirect(“login.xhtml”)时;登录页面会丢失所有资源、css、脚本等,因此页面显示时没有任何样式 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.or

我试图在JSF上建立一个基本的安全系统,如果用户没有登录并尝试访问一个受限页面,他将被重定向到login.xhtml。这是在servlet过滤器中完成的

我的问题是,当使用resp.sendRedirect(“login.xhtml”)时;登录页面会丢失所有资源、css、脚本等,因此页面显示时没有任何样式

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>
我使用的是PrimeFaces2.2.1、glassfish 3.1和JSF2.0

编辑:我刚刚注意到资源也使用了.xhtml扩展名,因此它干扰了过滤器

修复:
if(req.getSession().getAttribute(“func”)==null&&!pageRequested.contains(“login.xhtml”)&&&!pageRequested.contains(“/javax.faces.resource”))

响应sendRedirect(“login.xhtml”)

在JSF上重定向页面的正确方法,特别是在JSF 2中,是从方法调用返回视图的名称:

例如:

public String doLogin() {
    //apply the login logic.
    return "name/of/the/view?facesRedirect=true"
}

我经常使用这种方式直接从Java Bean重定向它:
FacesContext.getCurrentInstance().getExternalContext().redirect(Url)

您的过滤器也将HTTP请求重定向到JSF资源。如果当前请求不是JSF资源请求,则需要更改过滤器逻辑以添加检查。您可以通过检查请求URI是否在上下文路径之后以开头来实现这一点

if (request.getRequestURI().startsWith(request.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) {
    chain.doFilter(request, response);
}
else {
    response.sendRedirect(request.getContextPath() + "/login.xhtml");
}
 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
public String doLogin() {
    //apply the login logic.
    return "name/of/the/view?facesRedirect=true"
}
if (request.getRequestURI().startsWith(request.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) {
    chain.doFilter(request, response);
}
else {
    response.sendRedirect(request.getContextPath() + "/login.xhtml");
}