Jsf 函数多次被无故调用

Jsf 函数多次被无故调用,jsf,jakarta-ee,jsf-2,primefaces,Jsf,Jakarta Ee,Jsf 2,Primefaces,我有一个init()函数,它返回填充数据表所需的域列表,该数据表的每一行都有两个commandlinks,一个用于编辑,另一个用于删除,因此当我按下edit命令link时,一个对话框窗口将显示域信息,可以进行编辑(还有一个用于添加新域的对话框),所以问题是当我按edit时,init函数被调用了8次,然后在init函数被再次调用4次后,添加对话框的属性被设置(即使编辑命令链接和添加对话框窗口之间没有关系)。我不明白是什么。 这里是我的页面代码: <?xml version='1.0'

我有一个init()函数,它返回填充数据表所需的域列表,该数据表的每一行都有两个commandlinks,一个用于编辑,另一个用于删除,因此当我按下edit命令link时,一个对话框窗口将显示域信息,可以进行编辑(还有一个用于添加新域的对话框),所以问题是当我按edit时,init函数被调用了8次,然后在init函数被再次调用4次后,添加对话框的属性被设置(即使编辑命令链接和添加对话框窗口之间没有关系)。我不明白是什么。 这里是我的页面代码:

    <?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 value="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>

Facelet标题
方向:
这里的代码与Pastebin相同,但有点混乱:

这是我的备用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;
}
  ......
。。。。。。
公共列表initComb()
{
.....
}
公共列表初始化()
{
如果(choixDirection==0)
{
domaines=domainesService.getAllDomaines();
}
其他的
{
方向方向=方向服务.getDirections(choixDirection);
domaines=DomaineService.getDirDomaines(方向);
}
返回域;
}
公共作废保存()
{
......
}
公共作废删除()
{
......
}
公共无效更新()
{
......
}
//////////////////////////////////////////////////////////二传手和接球手\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
......
公共void setCodeDirection(整数codeDirection)
{
这个.codeDirection=codeDirection;
}
公共整数getCodeDirection()
{
返回码方向;
}
公共无效集合名域(字符串名域)
{
这个。nomDomaine=nomDomaine;
}
公共字符串getNomDomaine()
{
返回诺姆多明;
}
public void setNomDirecteur(字符串nomDirecteur)
{
这个.nomDirecteur=nomDirecteur;
}
公共字符串getNomDirecteur()
{
回报率;
}
......
}


我发布了很长的代码,原因是不知道问题来自何处。

您的错误是,您正在使用getter方法执行业务操作,而不是只调用一次的操作方法。您不应该在getter中执行业务操作。JSF EL使用getter访问bean属性。当它们被调用时,EL不会缓存它们getter只应返回bean属性(或者最多执行一些延迟加载或快速非密集计算,例如
return foo+bar;
)之类的简单求和),但绝对不能访问数据库或其他东西

将该作业移动到bean的构造函数或
@PostConstruct
方法或任何事件方法。在这种特殊情况下,您可能希望使用
@PostConstruct
@EJB
的帮助下预加载列表,并让ajax操作列表
@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;
}
<p:ajax process="topacteurs" listener="#{domaine.init}" update=":mainform:domainlist" />
...
<p:dataTable id="domainlist" var="e" value="#{domaine.domaines}">