Reputation: 527
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;
}
}
Upvotes: 2
Views: 625
Reputation: 527
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?
Upvotes: 1
Reputation: 7216
Seven calls to your bean are not the problem and JSF is doing it that way. I see a design problem here:
public ArrayList<Text> getTexts() {
ArrayList<Text> texts = new ArrayList<Text>();
texts.addAll(t.findAll());
return texts; }
Do not (re-)create your list in the getter of the backing bean! Your Primefaces datatable will not be sortable. You have to return the same List instance on all calls.
A simple solution would be to cache the list and create it lazily.
See this answer by the Primefaces god and lead developer: Datatable does not sort elements in primefaces
Upvotes: 2