Search code examples
jsf-2primefaceslifecycle

JSF lifecycle runs 7 times on page refresh


My web page has a primefaces datatable with a simple backing bean. I use a phase listener to log what is happening behind the scenes. Lo and behold, this simple page runs the JSF lifecyle no fewer than 7 times on every page refresh!!! What is going on?

<p:dataTable id="readers" var="reader" value="#{readerManagerBean.texts}" 
   rowKey="#{reader.id}" selection="#{readerBean.selectedText}" 
   selectionMode="multiple">  

  <p:column headerText="Reader Name" width="820">
      <h:outputText value="#{reader.name}" />
   </p:column>

</p:dataTable>

And here is the backing bean:

@ManagedBean
@RequestScoped
public class ReaderManagerBean {

private ArrayList<Text> texts;
private Text selectedText;
@EJB
private TextFacade t;

public Text getSelectedText() {
    return selectedText;
}

public void setSelectedText(Text selectedText) {
    this.selectedText = selectedText;
}

public ArrayList<Text> getTexts() {
    ArrayList<Text> texts = new ArrayList<Text>();
    texts.addAll(t.findAll());
    return texts;
}

public void setTexts(ArrayList<Text> texts) {
    this.texts = texts;
}

}


Solution

  • Oh... My... God...

    By trial and error I've found out what's going on.

    For the sake of brevity, I did not include all the code in my facelets page. Believe it or not, I get an entire JSF lifecycle running for every css page I link and every image I load. And it's all taking place in the head tag:

        <h:head>
        <link href="../resources/css/default.css" rel="stylesheet" type="text/css" />
        <link href="../resources/css/cssLayout.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="../resources/javascript/main.js"/>
    
        <script type="text/javascript">
            var preLoad = new Array();
            preLoad[0] = new Image();
            preLoad[0].src = '../resources/img/play_normal.png';
            preLoad[1] = new Image();
            preLoad[1].src = '../resources/img/play_hover.png';
            preLoad[2] = new Image();
            preLoad[2].src = '../resources/img/play_pressed.png';
    
            function switchImage(whichImage, imageNumber){
                document.images[whichImage].src = preLoad[imageNumber].src;
            }    
        </script>
    </h:head>
    

    So my revised question is this: how can I prevent these trivial lines of code executing the whole lifecycle?