最接近您可以获得完全相同的钩子的是
<f:view beforePhase="#{bean.beforePhase}" afterPhase="#{bean.afterPhase}">
和
public void beforePhase(PhaseEvent event) {
if (event.getPhaseId == PhaseId. RENDER_RESPONSE) {
// ...
}
}
public void afterPhase(PhaseEvent event) {
if (event.getPhaseId == PhaseId. RENDER_RESPONSE) {
// ...
}
}
preRender
可以以更简单的方式实现,将其放在您的视图中的任何位置:
<f:event type="preRenderView" listener="#{bean.preRenderView}" />
和
public void preRenderView(ComponentSystemEvent event) {
// ...
}
(参数是可选的,如果从不使用可以省略)
没有postRenderView
,但您可以轻松创建自定义事件。例如
@NamedEvent(shortName="postRenderView")
public class PostRenderViewEvent extends ComponentSystemEvent {
public PostRenderViewEvent(UIComponent component) {
super(component);
}
}
和
public class PostRenderViewListener implements PhaseListener {
@Override
public PhaseId getPhaseId() {
return PhaseId.RENDER_RESPONSE;
}
@Override
public void beforePhase(PhaseEvent event) {
// NOOP.
}
@Override
public void afterPhase(PhaseEvent event) {
FacesContext context = FacesContext.getCurrentInstance();
context.getApplication().publishEvent(context, PostRenderViewEvent.class, context.getViewRoot());
}
}
您注册faces-config.xml
为
<lifecycle>
<phase-listener>com.example.PostRenderViewListener</phase-listener>
</lifecycle>
那么你终于可以使用
<f:event type="postRenderView" listener="#{bean.postRenderView}" />
和
public void postRenderView(ComponentSystemEvent event) {
// ...
}