JSF:如何根据特定FacesContext会话属性的值将用户重定向到另一个页面 [英] JSF: How to redirect a user to a another page according to the value of a specific FacesContext session attribute

查看:98
本文介绍了JSF:如何根据特定FacesContext会话属性的值将用户重定向到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的JSF应用程序中,如果会话属性(例如userRole)的值为贡献者,则需要将用户从页面A重定向到页面B,例如,如果是作者,则需要将用户重定向到页面B. 。

In my JSF application, I need to redirect the user from Page A to page B if a session attribute such as userRole has a value of "contributor", and to a page C, for example, if it is "author".

我被告知我必须实现页面监听器或会话监听器。虽然我理解编写一个监听器类是非常简单和标准的,但我不知道如何在JSF页面上设置它(听会话)。

I'm told that I have to implement a page listener, or a session listener perhaps. Although I understand writing a listener class is quite straightforward and standard, I don't know how to set it up on the JSF page itself (to listen to the session).

任何人?

推荐答案

会话监听器( HttpSessionListener )不适合,因为它没有引用当前的HTTP请求/响应,这是更改请求/响应目标所必需的。

A session listener (HttpSessionListener) is unsuitable since it doesn't have a reference to the current HTTP request/response, which are mandatory in order to change the request/response destination.

使用过滤器。要了解有关过滤器的详情,请查看我们的 servlet-filters 标记信息页面。请注意,会话范围的JSF托管bean本身存储为 HttpSession 属性,并将托管bean名称作为键。您可以在 doFilter()方法中访问它们,如下所示:

Use a filter. To learn more about filters, check our servlet-filters tag info page. Note that session scoped JSF managed beans are by itself stored as HttpSession attribute with the managed bean name as key. You could access them in doFilter() method as follows:

Bean bean = (Bean) ((HttpServletRequest) request).getSession().getAttribute("bean");

或者当根据POST操作确定它时,只需在托管bean中返回不同的结果行动方法。然后只使用(隐式)JSF导航。伪:

Or when it's to be determined based on a POST action, just return a different outcome in the managed bean action method. Then just make use of (implicit) JSF navigation. Pseudo:

public String submit() {
    if (user is contributor) return "pageB";
    if (user is author) return "pageC";
    return "pageA";
}

这篇关于JSF:如何根据特定FacesContext会话属性的值将用户重定向到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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