我有三个 JSF 2.0 Web 模块,我需要在会话到期时重定向到登录页面。
我已经尝试使用它HttpSessionListener
,它正在调用sessionDestroyed()
事件方法,但我无法在其中转发/重定向请求。我认为这是因为没有HttpServletRequest
和HttpServletResponse
对象。
我也尝试过使用 a PhaseListener
,但它会导致网络浏览器中出现“重定向过多”错误。
public class SessionListener implements PhaseListener {
public PhaseId getPhaseId() {
return PhaseId.RESTORE_VIEW;
}
public void beforePhase(PhaseEvent event) {
if (!FacesContext.getCurrentInstance().isPostback()) {
try {
System.out.println("Session Destroyed");
FacesContext.getCurrentInstance().getExternalContext().redirect("login.jsf");
}
catch (Exception e) {
System.out.println("error" + e);
}
}
}
public void afterPhase(PhaseEvent event) {
try {
System.out.println("Session Created");
}
catch (Exception e) {
System.out.println("error" + e);
}
}
}
为什么这些尝试不起作用,我怎样才能最好地解决它?