页面加载时JSF重定向 [英] JSF redirect on page load

查看:127
本文介绍了页面加载时JSF重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短的问题:是否可以进行重定向,例如当用户未登录时,在呈现页面时?

Short question: Is it possible to do a redirection, say when a user isn't logged in, when a page is rendered?

推荐答案

为此,您应该使用 Filter

For that you should use a Filter.

例如

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { 
    if (((HttpServletRequest) request).getSession().getAttribute("user") == null) {
        ((HttpServletResponse) response).sendRedirect("error.jsf"); // Not logged in, so redirect to error page.
    } else {
        chain.doFilter(request, response); // Logged in, so just continue.
    }
}

在这里,我假定User像通常所期望的那样被放置在会话范围内.可以是名称为user的会话范围内的JSF托管bean.

Here I assume that the User is been placed in the session scope as you would normally expect. It can be a session scoped JSF managed bean with the name user.

导航规则不适用,因为在常规GET请求期间无法执行"bean操作".当要构造托管bean不能正常工作时,也要进行重定向,因为在正常的GET请求期间要构造托管bean时,响应已经开始呈现,这就是没有回报(只会产生IllegalStateException: response already committed).由于您实际上不需要侦听任何JSF阶段,因此,PhaseListener既麻烦又不堪重负.您只想侦听普通" HTTP请求以及会话范围内某个对象的存在.为此,过滤器是完美的.

A navigation rule is not applicable as there's no means of a "bean action" during a normal GET request. Also doing a redirect when the managed bean is about to be constructed ain't gong to work, because when a managed bean is to be constructed during a normal GET request, the response has already started to render and that's a point of no return (it would only produce IllegalStateException: response already committed). A PhaseListener is cumbersome and overwhelming as you actually don't need to listen on any of the JSF phases. You just want to listen on "plain" HTTP requests and the presence of a certain object in the session scope. For that a Filter is perfect.

这篇关于页面加载时JSF重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆