JSF 2中的Web过滤器如何?

我创建了这个过滤器:
public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException,servletexception {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpSession session = req.getSession();

        if (session.getAttribute("authenticated") != null || req.getRequestURI().endsWith("login.xhtml")) {
            chain.doFilter(request,response);
        } else {
            HttpServletResponse res = (HttpServletResponse) response;
            res.sendRedirect("login.xhtml");
            return;
        }

    }

    @Override
    public void init(FilterConfig filterConfig) throws servletexception {

    }

    @Override
    public void destroy() {
    }
}

这是我的结构:

然后我在web.xml中添加过滤器:

<filter>
    <filter-name>LoginFilter</filter-name>
    <filter-class>filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>LoginFilter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

过滤器按预期工作,但一直给我这个错误

"Was not possible find or provider the resource,login"

之后我的富有表面不再适用了.

我怎么解决这个问题?或者正确创建Web过滤器?

解决方法

传递给sendRedirect()的任何路径相对URL(即不以/开头的URL)都将相对于当前请求URI.我知道登录页面是在 http://localhost:8080/contextname/login.xhtml.所以,如果你例如访问 http://localhost:8080/contextname/pages/user/some.xhtml,那么这个重定向调用实际上将指向 http://localhost:8080/contextname/pages/user/login.xhtml,我认为这不存在.再次查看浏览器地址栏中的URL.

解决此问题,请改为重定向到域相对URL,即使用/启动URL.

res.sendRedirect(req.getcontextpath() + "/login.xhtml");

相关文章

Mip是什么意思以及作用有哪些
怎么测试Mip页面运行情况
MIP安装的具体步骤有哪些
HTML添加超链接、锚点的方法及作用详解(附视频)
MIP的规则有哪些
Mip轮播图组件中的重要属性讲解