PostRenderViewEvent: publising events after view is rendered
JSF 2.3 added a new ComponentSystemEvent, the so-called PostRenderViewEvent, it will be fired after the view is rendered in the RENDER_RESPONSE phase.
Create a sample to taste it.
Firstly create a backing bean.
1
@Model
2
public class PostRenderViewBean {
3
4
@Inject
5
Logger LOG;
6
7
public void init(PreRenderViewEvent e) {
8
LOG.log(Level.INFO, "fire PreRenderViewEvent:" + e);
9
}
10
11
public void execute() {
12
LOG.log(Level.INFO, "execute viewAction");
13
}
14
15
public void initialized(PostRenderViewEvent e) {
16
LOG.log(Level.INFO, "fire PostRenderViewEvent:" + e);
17
}
18
}
Copied!
In the facelets template, use f:event to declare events, and set type to postRenderView and listener to #{postRenderViewBean.initialized}.
1
<f:metadata>
2
<f:event type="preRenderView" listener="#{postRenderViewBean.init}" />
3
<f:viewAction action="#{postRenderViewBean.execute}" />
4
<f:event type="postRenderView" listener="#{postRenderViewBean.initialized}"/>
5
</f:metadata>
Copied!
viewAction was added in Java EE 7, check my Java EE 7 sample for details.
Run this application on Glassfish v5, open browser and navigate to http://localhost:8080/jsf-postrenderview-event/postRenderView.faces;
You will see the info in the IDE console.
1
Info: before phase:RESTORE_VIEW 1
2
Info: after phase:RESTORE_VIEW 1
3
Info: invoking custom ExceptionHandlder...
4
Info: before phase:APPLY_REQUEST_VALUES 2
5
Info: after phase:APPLY_REQUEST_VALUES 2
6
Info: invoking custom ExceptionHandlder...
7
Info: before phase:PROCESS_VALIDATIONS 3
8
Info: after phase:PROCESS_VALIDATIONS 3
9
Info: invoking custom ExceptionHandlder...
10
Info: before phase:UPDATE_MODEL_VALUES 4
11
Info: after phase:UPDATE_MODEL_VALUES 4
12
Info: invoking custom ExceptionHandlder...
13
Info: before phase:INVOKE_APPLICATION 5
14
Info: execute viewAction
15
Info: after phase:INVOKE_APPLICATION 5
16
Info: invoking custom ExceptionHandlder...
17
Info: before phase:RENDER_RESPONSE 6
18
Info: fire PreRenderViewEvent:javax.faces.event.PreRenderViewEvent[[email protected]]
19
Info: fire PostRenderViewEvent:javax.faces.event.PostRenderViewEvent[[email protected]]
20
Info: after phase:RENDER_RESPONSE 6
21
Info: invoking custom ExceptionHandlder...
Copied!
The viewAction is executed in INVOKE_APPLICATION phase, and PreRenderViewEvent and PostRenderViewEvent are fired in RENDER_RESPONSE phase.
Grab the source codes from my GitHub account, and have a try.
Last modified 8mo ago
Copy link