Search code examples
primefacesjsf-2.2view-scope

Primefaces datatable and ViewScoped


I'm using primefaces 5.0 on wildfly 8.2.0 (mojarra 2.2.8).

I tried to use a simple primefaces datatable with expansion but each time I expand a row, my backed bean @PostConstruct is triggered (which reloads the data which nullifies the use of @ViewScoped in the first place).

I've seen other questions on stackoverflow about this problem but no solution worked for me:

  • I'm using JSF 2.2+
  • I'm not using any JSTL tags
  • I disabled partial state saving in web.xml
  • I tried using different @ViewScoped (bean, view and even omnifaces'one)

My bean:

@Named
@javax.faces.view.ViewScoped
@SuppressWarnings("serial")
public class TestBean implements Serializable {

    private List<String> things;

    @PostConstruct
    public void initialize() {
        System.out.println("initializing...");
        this.things = Arrays.asList("michael", "david", "paul");
    }

    public List<String> getThings() {
        return this.things;
    }
}

My template:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Test</title>
    </h:head>
    <h:body>
        <p:dataTable value="#{testBean.things}" var="thing">
            <p:column>
                <p:rowToggler />
            </p:column>
            <p:column>
                <h:outputText value="#{thing}" />
            </p:column>
            <p:rowExpansion>
                <h:outputText value="#{thing}" />
            </p:rowExpansion>
        </p:dataTable>
    </h:body>
</html>

Solution

  • To work, <p:dataTable> has to be inside a <h:form>.