Search code examples
jsf-2faceletsomnifaces

Why is Omnifaces postInvokeAction listener method called twice


I am new to using omnifaces. From the little I gathered, the postInvokeAction is a better choice to use when handling objects in flashscope rather than the jsf preRenderView event. But what I noticed is that the listener method is been called twice! I felt that the preInvokeAction is analogous to the before phase listener and the postInvokeAction analogous to the after phase listener equivalent of the PhaseID.INVOKE_APPLICATION and as such should be called only once for the corresponding event. Is this correct? Kindly explain to me please.

I currently run on Mojarra 2.1.17 and Omnifaces 1.3.

Thank you in anticipation for your replies!

layout1.html

    <h:body>
    <p style="color: blue; font-size: 12pt;">1. This is the main content of the file.</p>
    <ui:insert name="body_contents"/>
    <p style="color: blue; font-size: 12pt;">This is the the remaining part of the document...   in layout1</p>
    </h:body>

samplepage2.html

<h:body>
    <f:view>
        <f:metadata>
            <f:viewParam name="dummy_var" value="#{sampletest.val_test}"/>
            <f:event type="postInvokeAction" listener="#{sampletest.frompostinvokeaction}" />
            <f:event type="preRenderView" listener="#{sampletest.fromprerenderview}" />
        </f:metadata>
    </f:view>
    <ui:composition template='/layout1.html'>
        <ui:define name="title_on_head">
            <style type="text/css">
                .pkssd{
                    min-width: 340px; min-height: 30px; background: appworkspace; color: blue; font-size: 11pt; font-style: italic;
                }
            </style>
        </ui:define>
        <ui:define name="body_contents">
            <p class="pkssd">This is the active content...</p>
        </ui:define>
    </ui:composition>
</h:body>

SampleTest.java

@ManagedBean(name="sampletest")
@ViewScoped
public class SampleTest {
private String val_test;    public void frompostinvokeaction(){
    System.out.println("frompostinvokeaction: val_test: " + val_test);    }
public void fromprerenderview(ComponentSystemEvent cse){
    System.out.println("fromprerenderview : val_test: " + val_test);
}
}

Solution

  • The way how you used master/client templates is not entirely correct. It's completely off from the specification and may have caused unspecified behavior.

    The proper way would be this:

    /WEB-INF/layout1.html

    <!DOCTYPE html>
    <html lang="en"
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:ui="http://java.sun.com/jsf/facelets"
    >
        <f:view>
            <ui:insert name="metadata" />
            <h:head>
                ...
                <ui:insert name="title_on_head" />
                ...
            </h:head>
            <h:body>
                ...
                <ui:insert name="body_contents" />
                ...
            </h:body>
        </f:view>
    </html>
    

    /samplepage2.html

    <ui:composition template="/WEB-INF/layout1.html">
        <ui:define name="metadata">
            <f:metadata>
                <f:viewParam name="dummy_var" value="#{sampletest.val_test}"/>
                <f:event type="postInvokeAction" listener="#{sampletest.frompostinvokeaction}" />
                <f:event type="preRenderView" listener="#{sampletest.fromprerenderview}" />
            </f:metadata>
        </ui:define>
    
        <ui:define name="title_on_head">
            <style type="text/css">
                .pkssd{
                    min-width: 340px; min-height: 30px; background: appworkspace; color: blue; font-size: 11pt; font-style: italic;
                }
            </style>
        </ui:define>
    
        <ui:define name="body_contents">
            <p class="pkssd">This is the active content...</p>
        </ui:define>
    </ui:composition>
    

    (yes, that's the complete file, there's nothing outside <ui:composition>)

    See also: