@ViewScoped @PostContruct is called for every ajax request

Using Primefaces 5.0 JSF 2.2.7 deployed on EAP 6.1.

I have this Managed Bean below.

import hh.bean.Service;
import hh.dao.ServiceDao;
import hh.dao.impl.ServiceDaoImpl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class View1 implements Serializable {

    private static final long serialVersionUID = 1L;

    private ServiceDao serviceDao = new ServiceDaoImpl();

    @PostConstruct
    public void init() {
        System.out.println(View1.class.getName() + ": init() " + this);
    }

    public List<Service> getServices(){
        return serviceDao.getAllServices();
    }
}

      

I am calling it from the xhtml below.

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <title>Home Web</title>
    <f:facet name="first">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta http-equiv="Content-Type"
            content="text/html; charset=UTF-8" />
        <meta name="viewport"
            content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0" />
        <meta name="apple-mobile-web-app-capable" content="yes" />
    </f:facet>
</h:head>

<h:body>
    <h:outputStylesheet library="css" name="newcss.css" />
    <p:dataTable var="service" value="#{view1.services}">
        <p:column style="width:16px">
            <p:rowToggler />
        </p:column>
        <p:column headerText="Id">
            <h:outputText value="#{service.id}" />
        </p:column>

        <p:column headerText="xxxx">
            <h:outputText value="#{service.description}" />
        </p:column>

        <p:rowExpansion>
            <p:dataTable var="sv" value="#{view1.services}">
                <p:column headerText="Id">
                    <h:outputText value="#{sv.id}" />
                </p:column>
            </p:dataTable>
        </p:rowExpansion>
    </p:dataTable>
</h:body>
</html>

      

I noticed that every time I expand the string, I get called init()

. I thought I @ViewScoped

lived when the request stays on the same page.

When I switch to @SessionScoped

, is init()

not called when I expand the string.

Edit 1: Paste in all xhtml, specify jsf version / impl

Edit 2: Fixed issues surrounding p:dataTable

with h:form

. Not sure why this is fixed ...

+3


source to share


1 answer


Fixed these issues by surrounding p: dataTable with h: form. Not sure why this is fixed ...

JSF view state is maintained by a javax.faces.ViewState

hidden input field <h:form>

. If you don't <h:form>

, then PrimeFaces will not be able to find this hidden input field to pass its value along with the jQuery ajax request.



If this information is missing from the request (ajax), then JSF will just create a completely new view and essentially have view beans associated with it as well .

+6


source







All Articles