MarkL
MarkL

Reputation: 478

Listener on Omnifaces preInvokeAction/postInvokeAction not invoked without viewParam

I'm not 100% sure this isn't as designed, but it appears that a preInvokeAction listener in the view metadata is only invoked if a viewParam is present.

The following works:

<f:metadata>
    <f:viewParam name="test" value="#{myForm.myValue}"/>
    <f:event type="preInvokeAction" listener="#{myController.initializeForm}"/>
</f:metadata>

But this does not:

<f:metadata>
    <f:event type="preInvokeAction" listener="#{myController.initializeForm}"/>
</f:metadata>

It may be that the invokeAction phase does not exist if there are no view parameters so it doesn't make sense, but I'm not familiar enough with the lifecycle at this level to know if that's the case or if this is just a miss.

A workaround is to just put in a bogus parameter.

Does anyone know if this is a bug or as expected?

Upvotes: 1

Views: 144

Answers (1)

BalusC
BalusC

Reputation: 1109532

It's indeed working as designed.

Those events are triggered during INVOKE_APPLICATION phase. When there are no view parameters discovered during RESTORE_VIEW phase, then JSF will immediately advance to RENDER_RESPONSE phase. All phases in between will be skipped: APPLY_REQUEST_VALUES, PROCESS_VALIDATIONS, UPDATE_MODEL_VALUES and INVOKE_APPLICATION.

The work around is indeed to declare a "dummy" <f:viewParam>, or better, use the JSF 2.2 provided <f:viewAction> instead as mentioned in InvokeActionEventListener documentation.

<f:viewAction action="#{myController.initializeForm}" />

Do note that a <f:viewParam> does not necessarily require a bean property.

<f:viewParam name="test" />

It will implicitly be placed in request scope instead and be available as #{test}.

See also:

Upvotes: 1

Related Questions