I have a problem where the getter of a datatable is called multiple times, and I see thats fine here Primefaces dataTable call method multiple times when click commandButton. why?
The problem is that is giving me a datatable with no results when there is actually results, because at somepoint it will be null, I did a workaround for this with a static variable but I don't know if this can give me a problem in the future (please read im gonna put you in context with the question). here is my code of the datatable:
<p:dialog id="dialogMenu" appendTo="@(body)" widgetVar="dialogMenuwv"
closable="true" closeOnEscape="true">
<h:form id="formDialogMenu">
<p:dataTable value="#{homeView.data1}" var="plato" id="tablaMenu"
resizableColumns="false" tableStyle="width:auto"
emptyMessage="No hay platos disponibles" paginator="true" rows="5">
<f:facet name="header">
Platos
</f:facet>
<p:column filterBy="#{plato.activo}" sortBy="#{plato.activo}">
<f:facet name="header">
<h:outputText value="Activo" />
</f:facet>
<h:outputText value="#{plato.activo}" />
</p:column>
<p:column filterBy="#{plato.nombre}" sortBy="#{plato.nombre}">
<f:facet name="header">
<h:outputText value="Nombre" />
</f:facet>
<h:outputText value="#{plato.nombre}" />
</p:column>
<p:column filterBy="#{plato.precio}" sortBy="#{plato.precio}">
<f:facet name="header">
<h:outputText value="Precio" />
</f:facet>
<h:outputText value="#{plato.precio}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Imagen" />
</f:facet>
<p:graphicImage alt="image" width="100%" height="50%"
value="#{plato.imagenPlato}" cache="false">
</p:graphicImage>
</p:column>
</p:dataTable>
</h:form>
</p:dialog>
As you can see the table is inside a dialog the one is opened when this button is clicked:
<p:commandButton value="Ver Menu" update="formDialogMenu:tablaMenu"
actionListener="#{homeView.getData1()}"
onclick="PF('dialogMenuwv').show()"></p:commandButton>
I had to add that actionListener and update to my button because I notice that leaving it only with the onlick to show the dialog wouldn't work and I followed advice from linked answer.
Finally here is my getter method as I expected it to work and it would work if it was only called when the marker was selected (Im working with gMap):
public List<PlatoDTO> getData1() {
try {
if (data1 == null) {
// puestocomida isn't null when the marker is selected but since the getter is being called so many times, puestocomida will be null later and so data1 will return null.
data1 = businessDelegatorView.getDataPlatoPuesto(puestocomida.getPueId());
}
} catch (Exception e) {
e.printStackTrace();
}
return data1;
}
while debugging I notice that the getter is called multiple times and for some reason my puestocomida variable becomes null, I guess it is because no marker selected anymore, so an exception is throwed there and I get a null returned. So I decided to make a static variable that will store my puestocomida.getpueId() and so I modified the getter like this:
// I made this static to store the id I need for the getter
private static Integer pueId;
public List<PlatoDTO> getData1() {
try {
data1 = businessDelegatorView.getDataPlatoPuesto(pueId);
} catch (Exception e) {
e.printStackTrace();
}
return data1;
}
and it is working as I needed it to work like this, but my question is, this variable static will be different or the same for each user that goes into the application? Because it varies depending on the marker that a user selects, I have a googleMap and depending on the marker that a user selects "puestodecomida" is set, and im storing its id on that static variable, this static only updates when a marker is selected.
public void onMarkerSelect(OverlaySelectEvent event) {
// Just making a Puestocomida object out of the selected marker
marker = (Marker) event.getOverlay();
Puestocomida puestocomida = (Puestocomida) marker.getData();
this.puestocomida = puestocomida;
try {
// Saving the puestocomida id in the static variable
data1 = businessDelegatorView.getDataPlatoPuesto(puestocomida.getPueId());
pueId = puestocomida.getPueId();
} catch (Exception e1) {
e1.printStackTrace();
}
puestocomida is restaurant in english and it varies on the marker user selects. any advice is appreciated :)