问题
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