问题描述
当我的托管bean中的特定方法被调用时,我想知道我是否处于JSF生命周期的恢复视图阶段。我怎样才能做到这一点?
When a particular method in my managed bean gets called, I want to know whether I'm in the Restore View phase of the JSF Lifecycle. How can I do this?
推荐答案
如果您已经使用JSF 2.0,那么可以使用:
If you're already on JSF 2.0, then you can check it using FacesContext#getCurrentPhaseId()
:
if (FacesContext.getCurrentInstance().getCurrentPhaseId() == PhaseId.RESTORE_VIEW) {
// Restore view called.
}
但如果你还在使用JSF 1.x,那么你最好度假村正在使用,在 beforePhase期间在请求范围内设置标记/切换/标记()
并在 afterPhase()
期间删除它。让bean的getter方法检查它在请求范围中的存在。
But if you're still on JSF 1.x yet, then your best resort is using a PhaseListener
which listens on PhaseId.RESTORE_VIEW
, sets a flag/toggle/token in the request scope during beforePhase()
and removes the it during afterPhase()
. Let the bean's getter method check its presence in the request scope then.
那就是说,你究竟需要它做什么?我从来没有需要这样的功能要求。是不是bean的构造函数或 @PostConstruct
带注释的方法可能是一个更好的地方来做这样的初始化?
That said, what exactly do you need it for? I've never had the need for such a functional requirement. Isn't the bean's constructor or an @PostConstruct
annotated method probably a better place to do initialization stuff like this?
这篇关于JSF:如何检查我是否处于“恢复视图”状态相?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!