问题描述
我的问题类似于不以任何方式涉及 PrimeFaces
。另外,我找不到真正的解决方案。
My problem is similar to the one here but doesn't involve PrimeFaces
in any way. Also, I couldn't find a real solution there.
我基本上试图让phaselistener重定向到登录页面以防万一没有JSF会话(当例如,会话已超时)
I'm basically trying to get the phaselistener to redirect to login page in case there is no JSF session (When, for example, session has timed out)
我正在尝试从JSF 2 Phaselistener中重定向。总而言之,我正在做的是:
I'm trying to redirect from within a JSF 2 Phaselistener. To sum up, what I'm doing is this:
public void beforePhase(PhaseEvent event) {
PhaseId id = event.getPhaseId();
if(id.equals(PhaseId.RESTORE_VIEW)){
FacesContext context = event.getFacesContext();
Map<String, Object> sessionMap = context.getExternalContext().getSessionMap();
if(sessionMap==null || sessionMap.isEmpty()){
// No Session, Redirect to login
try {
context.getExternalContext().redirect("/login");
} catch (Exception e) {
...
}
}
}
}
当重定向代码运行时,我得到以下异常:
When the redirect code runs I get this exception:
java.lang.NullPointerException
at org.apache.myfaces.context.servlet.PartialViewContextImpl.getPartialResponseWriter(PartialViewContextImpl.java:301)
at org.apache.myfaces.context.servlet.ServletExternalContextImpl.redirect(ServletExternalContextImpl.java:452)
at com.AuthenticationPhaseListener.userIsNotLogedIn
可能导致此问题?我做错了吗?
What could be causing this? Am I doing this wrong?
谢谢!
推荐答案
这似乎在ajax请求期间发生。我不确定确切原因,堆栈跟踪至少表明MyFaces实现中可能存在错误。
This seem to be happening during an ajax request. I'm not sure about the exact cause, the stacktrace at least indicates a possible bug in MyFaces implementation.
至少,整体设计方法很差。这种HTTP请求/响应修改最好不发生在 PhaseListener
中。在那里它不是为了。您希望在正常的而不是。
At least, the overall design approach is poor. This kind of HTTP request/response modifications should preferably not take place in a PhaseListener
. There it is not intended for. You want to do this kind of job in a normal servlet Filter
instead.
这篇关于无法从JSF phaselistener重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!