JSF: How to redirect a user to a another page according to the value of a specific FacesContext session attribute

a 夏天 提交于 2020-01-01 19:28:11

问题


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".

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).

Anybody?


回答1:


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.

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");

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";
}


来源:https://stackoverflow.com/questions/4958154/jsf-how-to-redirect-a-user-to-a-another-page-according-to-the-value-of-a-specif

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!