Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to implement the <a href="http://docs.oracle.com/javaee/6/api/javax/faces/event/PhaseListener.html" rel="noreferrer"><code>PhaseListener</code></a> interface and hook on <a href="http://docs.oracle.com/javaee/6/api/javax/faces/event/PhaseListener.html#beforePhase%28javax.faces.event.PhaseEvent%29" rel="noreferrer"><code>beforePhase()</code></a> of the <a href="http://docs.oracle.com/javaee/6/api/javax/faces/event/PhaseId.html#RENDER_RESPONSE" rel="noreferrer"><code>PhaseId_RENDER_RESPONSE</code></a>. The render response is the last phase of the JSF lifecycle.</p> <pre class="lang-java prettyprint-override"><code>public class MyPhaseListener implements PhaseListener { @Override public PhaseId getPhaseId() { return PhaseId.RENDER_RESPONSE; } @Override public void beforePhase(PhaseEvent event) { // Do your job here which should run right before the RENDER_RESPONSE. } @Override public void afterPhase(PhaseEvent event) { // Do your job here which should run right after the RENDER_RESPONSE. } } </code></pre> <p>To get it to run, register it as follows in <code>faces-config.xml</code>:</p> <pre class="lang-xml prettyprint-override"><code>&lt;lifecycle&gt; &lt;phase-listener&gt;com.example.MyPhaseListener&lt;/phase-listener&gt; &lt;/lifecycle&gt; </code></pre> <hr> <p><strong>Update</strong> the above phase listener is indeed applicaiton-wide. To have a phase listener for a specific view, use the <code>beforePhase</code> and/or <code>afterPhase</code> attributes of the <a href="http://download.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/f/view.html" rel="noreferrer"><code>&lt;f:view&gt;</code></a>.</p> <p>E.g.</p> <pre class="lang-html prettyprint-override"><code>&lt;f:view beforePhase="#{bean.beforePhase}"&gt; ... &lt;/f:view&gt; </code></pre> <p>with</p> <pre><code>public void beforePhase(PhaseEvent event) { if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) { // Do here your job which should run right before the RENDER_RESPONSE. } } </code></pre> <p>A more JSF 2.0 way is by the way using the <a href="http://download.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/f/event.html" rel="noreferrer"><code>&lt;f:event type="preRenderView"&gt;</code></a>:</p> <pre class="lang-html prettyprint-override"><code>&lt;f:event type="preRenderView" listener="#{bean.preRenderView}" /&gt; </code></pre> <p>with</p> <pre><code>public void preRenderView() { // Do here your job which should run right before the RENDER_RESPONSE. } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload