function called many times for no reason
i have an init() function that returns a list of Domaines needed to populate a datatable, each row of this datatable have two commandlinks one for edit and another for delete, so when i press the edit command link a dialog window apear showing the Domaine information whith the possibility of editing (there is another dialog for adding new domaines), so the problem is when i press edit the init function is called 8 times then the attributes of the adding dialog are setted (even if there is no relation between the thee edit command link and the adding dialog window) after the init function is called again 4 times. i don't understand wh. here my page code:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<ui:composition template="gpsiTemplate.xhtml">
<ui:define name="left-menu">
<ui:include src="admin-menuGauche.xhtml" />
</ui:define>
<ui:define name="top">
<ui:include src="menu-top.xhtml" />
</ui:define>
<ui:define name="content-top">
<center>
<f:view>
<h:form id="topacteurs">
<h:panelGrid columns="14" styleClass="adminTopTable" >
<h5 class="calendar">Direction :</h5>
<p:selectOneMenu id="typeacteurs" value="#{domaine.choixDirection}" style="width: 180px">
<f:selectItem itemLabel="Direction..." itemValue="" />
<f:selectItems value="#{direction.initcomb()}" var="ta" itemValue="#{ta.codeDirection}" itemLabel="#{ta.libelleDirection}" />
<p:ajax update=":mainform:domainlist" process="topacteurs" event="change" />
</p:selectOneMenu>
<p:spacer width="20" height="0" />
<p:commandButton title="Ajouter un domaine" image="ui-icon ui-icon-disk" value="Ajouter un domaine" oncomplete="ajoutDomaine.show()"/>
</h:panelGrid>
</h:form>
</f:view>
</center>
</ui:define>
<ui:define name="content-content">
<h:form id="modifform">
<p:dialog header="Modifier un domaine" widgetVar="editDomaine" modal="true" width="400" height="200" showEffect="clip" hideEffect="clip">
<p:outputPanel id="editDomaineDetails" style="text-align:center;" layout="block">
<center>
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel value="Intitulé :"/>
<p:inputText value="#{domaine.currentDomaine.libelleDomaine}" style="width: 180px"/>
<h:outputLabel value="Respensable :"/>
<p:inputText value="#{domaine.currentDomaine.nomDirecteur}" style="width: 180px"/>
<h:outputLabel value="Direction :"/>
<p:selectOneMenu id="editchoidirection" value="#{domaine.codeDirection}" style="width: 180px">
<f:selectItem itemLabel="Direction..." itemValue="" />
<f:selectItems value="#{direction.initcomb()}" var="ta" itemValue="#{ta.codeDirection}" itemLabel="#{ta.libelleDirection}" />
</p:selectOneMenu>
</h:panelGrid>
<h:panelGrid columns="2" cellpadding="5">
<p:commandButton value="Modifier" update="messages editDomaineDetails :mainform:domainlist" actionListener="#{domaine.update()}" oncomplete="editDomaine.hide()"/>
<p:commandButton value="Annuler" oncomplete="editDomaine.hide()"/>
</h:panelGrid>
</center>
</p:outputPanel>
</p:dialog>
</h:form>
<h:form id="mainform">
<p:growl id="messages" showDetail="true"/>
<p:confirmDialog message="Etes-vous sure?" width="200"
showEffect="clip" hideEffect="clip"
header="Confirmation" severity="alert" widgetVar="confirmation">
<p:commandButton value="Oui sure" update="messages :mainform:domainlist" actionListener="#{domaine.delete()}" oncomplete="confirmation.hide()"/>
<p:commandButton value="Non" onclick="confirmation.hide()" type="button" />
</p:confirmDialog>
<p:dialog header="Ajouter un domaine" widgetVar="ajoutDomaine" modal="true" width="400" height="200" showEffect="clip" hideEffect="clip">
<p:outputPanel id="ajoutDomaineDetails" style="text-align:center;" layout="block">
<center>
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel value="Intitulé :"/>
<p:inputText value="#{domaine.nomDomaine}" style="width: 180px"/>
<h:outputLabel value="Respensable :"/>
<p:inputText value="#{domaine.nomDirecteur}" style="width: 180px"/>
<h:outputLabel value="Direction :"/>
<p:selectOneMenu id="ajoutchoidirection" value="#{domaine.codeDirection}" style="width: 180px">
<f:selectItem itemLabel="Direction..." itemValue="" />
<f:selectItems value="#{direction.initcomb()}" var="ta" itemValue="#{ta.codeDirection}" itemLabel="#{ta.libelleDirection}" />
</p:selectOneMenu>
</h:panelGrid>
<h:panelGrid columns="2" cellpadding="5">
<p:commandButton value="Ajouter" update="messages ajoutDomaineDetails :mainform:domainlist" actionListener="#{domaine.save()}" oncomplete="ajoutDomaine.hide()"/>
<p:commandButton value="Annuler" oncomplete="ajoutDomaine.hide()"/>
</h:panelGrid>
</center>
</p:outputPanel>
</p:dialog>
<p:dataTable id="domainlist" var="e" value="#{domaine.init()}">
<p:column headerText="Intitulé" filterBy="#{e.libelleDomaine}">
<h:outputText value="#{e.libelleDomaine}" />
</p:column>
<p:column headerText="Directeur" filterBy="#{e.nomDirecteur}">
<h:outputText value="#{e.nomDirecteur}" />
</p:column>
<p:column headerText="Direction" filterBy="#{e.directions.libelleDirection}">
<h:outputText value="#{e.directions.libelleDirection}" />
</p:column>
<p:column>
<h:panelGrid columns="3" style="border-color: #ffffff">
<p:commandLink update=":modifform:editDomaineDetails" title="Editer" oncomplete="editDomaine.show()">
<p:graphicImage value="resources/images/edit.png"/>
<f:setPropertyActionListener value="#{e}" target="#{domaine.currentDomaine}" />
</p:commandLink>
<p:commandLink title="Supprimer" oncomplete="confirmation.show()">
<p:graphicImage value="resources/images/delete.png"/>
<f:setPropertyActionListener value="#{e}" target="#{domaine.currentDomaine}" />
</p:commandLink>
</h:panelGrid>
</p:column>
</p:dataTable>
<p:stack icon="resources/images/stack/stck.png">
<p:menuitem value="Photo" icon="resources/images/stack/photo.png" url="http://localhost:8084/Gpsi/faces/profile-Photo.xhtml"/>
<p:menuitem value="Profile" icon="resources/images/stack/profile.png" url="http://localhost:8084/Gpsi/faces/profile.xhtml"/>
<p:menuitem 开发者_如何学Pythonvalue="Administration" icon="resources/images/stack/administration.png" url="http://localhost:8084/Gpsi/faces/admin.xhtml"/>
</p:stack>
</h:form>
</ui:define>
</ui:composition>
</h:body>
</html>
here the same code on Pastebin but it's a bit messed up: http://pastebin.com/W0XGa0d2
and here is my back up bean:
......
public List<Domaines> initComb()
{
.....
}
public List<Domaines> init()
{
if(choixDirection==0)
{
domaines=domainesService.getAllDomaines();
}
else
{
Directions direction=directionsService.getDirections(choixDirection);
domaines=domainesService.getDirDomaines(direction);
}
return domaines;
}
public void save()
{
......
}
public void delete()
{
......
}
public void update()
{
......
}
////////////////////////////////////////////////////////// setters & getters \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
......
public void setCodeDirection(Integer codeDirection)
{
this.codeDirection=codeDirection;
}
public Integer getCodeDirection()
{
return codeDirection;
}
public void setNomDomaine(String nomDomaine)
{
this.nomDomaine=nomDomaine;
}
public String getNomDomaine()
{
return nomDomaine;
}
public void setNomDirecteur(String nomDirecteur)
{
this.nomDirecteur=nomDirecteur;
}
public String getNomDirecteur()
{
return nomDirecteur;
}
......
}
i posted long codes cause don't where the problem is coming from.
Your mistake is that you're doing business action in a getter method instead of an action method which is called only once. You should not do business actions in getters. JSF EL uses getters to access bean properties. EL does not cache them when they're ever called once. Getters should solely return bean properties (or at highest do some lazy loading or quick and non-intensive calculation, e.g. a simple sum like return foo + bar;
), but definitely not access the database or something.
Move that job to the constructor or @PostConstruct
method or any event method of the bean. In this particular case, you likely want to use @PostConstruct
to preload the list with help of an @EJB
and let the ajax action listener point to the same method as well. In a @ViewScoped
bean, this way it's called only once during first request of the view and also called only once when you change the selection.
@PostConstruct
public void init() {
if (choixDirection == 0) {
domaines = domainesService.getAllDomaines();
} else {
Directions direction = directionsService.getDirections(choixDirection);
domaines = domainesService.getDirDomaines(direction);
}
}
public List<Domaines> getDomaines() {
return domaines;
}
and fix the view accordingly
<p:ajax process="topacteurs" listener="#{domaine.init}" update=":mainform:domainlist" />
...
<p:dataTable id="domainlist" var="e" value="#{domaine.domaines}">
See also:
- Why JSF calls getters multiple times
精彩评论