1

I've found a strange behaviour while using f:viewAction in ui:composition.

I've a main page that includes a composition. In both pages I've defined an f:viewAction, but when loading page, only the one in the main page is fired.

This is an example of the code I used:

Main.xhtml

<h:head>
    <h:outputLabel value="Test page*******" />
</h:head>
<h:body>
    <ui:include src="/pages/include.xhtml" />
</h:body>
</html>

include.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html">

    <f:metadata>
        <f:viewAction action="#{sample.actionTest2}" />
    </f:metadata>

    <h:outputLabel value="Include" />

</ui:composition>

Sample bean

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "sample")
@SessionScoped
public class SampleBean implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 2458168772774828175L;

    public void actionTest1() {
        System.out.println("Test1");
    }

    public void actionTest2() {
        System.out.println("Test2");
    }
}

Only sample.actionTest1 is invoked.

The JSF version is Mojarra 2.2.8-02

I'm editing my question to explain in details my scenario. In the previous question I omitted, for simplicity's sake, the fact that I need to render only the content section.

The modified main page is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html">
<f:metadata>
    <f:viewAction action="#{sample.actionTest1}" />
</f:metadata>

<h:head>
    <h:outputLabel value="Test page*******" />
    <h:form>
        <h:commandLink value="Refresh content">
            <f:ajax render=":content" />
        </h:commandLink>
    </h:form>
</h:head>
<h:body>
    <h:panelGroup id="content">
        <ui:include src="/pages/include.xhtml" />
    </h:panelGroup>
</h:body>
</html>

As you can see, when command link in clicked, only content must be rendered. Is there any possibility to use f:viewAction to execute some action before rendering in this scenario? Now I'm using f:event and it works pefectly.

0 Answers0