JSF 2 equivalent of IBM's hx:scriptCollector pre- and postRender

谁说我不能喝 提交于 2019-12-22 09:15:18

问题


I am migrating an old JSF application from WebSphere to JBoss: the old version uses an IBM implementation of JSF. My question concerns the following component:

<hx:scriptCollector id="aScriptCollector"
        preRender="#{aBean.onPageLoadBegin}" postRender="#{aBean.onPageLoadEnd}">

To reproduce the preRender behavior in JSF 2 I use a binding for the form (s. here). My questions:

1) Do you know a trick for simulating postRender in JSF 2?

2) Do you think is the trick I am using for preRender "clean"?

Thanks a lot for your help! Bye


回答1:


Closest what you can get to achieve exactly the same hooks is

<f:view beforePhase="#{bean.beforePhase}" afterPhase="#{bean.afterPhase}">

with

public void beforePhase(PhaseEvent event) {
    if (event.getPhaseId == PhaseId. RENDER_RESPONSE) {
        // ...
    }
}

public void afterPhase(PhaseEvent event) {
    if (event.getPhaseId == PhaseId. RENDER_RESPONSE) {
        // ...
    }
}

The preRender can be achieved in an easier manner, put this anywhere in your view:

<f:event type="preRenderView" listener="#{bean.preRenderView}" />

with

public void preRenderView(ComponentSystemEvent event) {
    // ...
}

(the argument is optional, you can omit it if never used)


There's no such thing as postRenderView, but you can easily create custom events. E.g.

@NamedEvent(shortName="postRenderView")
public class PostRenderViewEvent extends ComponentSystemEvent {

    public PostRenderViewEvent(UIComponent component) {
        super(component);
    }

}

and

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());
    }

}

which you register in faces-config.xml as

<lifecycle>
    <phase-listener>com.example.PostRenderViewListener</phase-listener>
</lifecycle>

then you can finally use

<f:event type="postRenderView" listener="#{bean.postRenderView}" />

with

public void postRenderView(ComponentSystemEvent event) {
    // ...
}


来源:https://stackoverflow.com/questions/13118103/jsf-2-equivalent-of-ibms-hxscriptcollector-pre-and-postrender

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!