1

the problem when i try to insert many rows on my datatable there's a problem of displaying data: the insered row is displayed 2 times on the datatable

for exemple :

                     insert a second row
              row[1]  ===============>  row[2] 
                                        row[2] 

Facelet:

 <h:form>
    <p:commandButton id="dlgAjout" value="Ajout Agent" update=":displayCre" oncomplete="dialogCre.show();" icon="icon-add" title="Ajout  Agent" />
</h:form>
<h:form id="formdatable">
    <p:dataTable id="agentList" value="#{agentBean.agentministeres }" var="item" paginator="true" rows="10" paginatorTemplate="{CurrentPageReport}  {FirstPageLink}  {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" rowsPerPageTemplate="5,10,15">
        <p:column>
            <f:facet name="header">
                <h:outputText value="NomAgent" />
            </f:facet>
            <h:outputText value="#{item.nomAgent}" />
        </p:column>
        <p:column>
            <f:facet name="header">
                <h:outputText value="PrenomAgent" />
            </f:facet>
            <h:outputText value="#{item.prenomAgent}" />
        </p:column>
        <p:column>
            <f:facet name="header">
                <h:outputText value="Idagent" />
            </f:facet>
            <h:outputText value="#{item.idagent}" />
        </p:column>
        <f:facet name="footer">En Total il existe : #{ fn:length(agentBean.agentministeres)} agents.</f:facet>
    </p:dataTable>
</h:form>

<h:form id="displayCre">
    <p:dialog header="Ajout Agent" widgetVar="dialogCre" resizable="false" id="dlgAjout" showEffect="fade" hideEffect="explode" modal="true">
        <h:panelGrid id="display" columns="2" cellpadding="4" style="margin:0 auto;">
            <h:outputLabel value="NomAgent:" for="nomAgent" />
            <h:inputText id="nomAgent" value="# {agentBean.agentministere.nomAgent}" title="NomAgent" />
            <h:outputLabel value="PrenomAgent:" for="prenomAgent" />
            <h:inputText id="prenomAgent" value="#{agentBean.agentministere.prenomAgent}" title="PrenomAgent" />
            <h:outputLabel value="Idagent:" for="idagent" />
            <h:inputText id="idagent" value="#{agentBean.agentministere.idagent}" title="Idagent" required="true" requiredMessage="The Idagent field is required." />
            <f:facet name="footer">
                <p:commandButton id="confirm" title="Valider" value="Valider" action="#{agentBean.enregistrerAgent}" oncomplete="dialogCre.hide()" update=":formdatable, :messages" icon="icon-save" />
            </f:facet>
        </h:panelGrid>
    </p:dialog>
</h:form>

the entity

 public class Agentministere implements Serializable {

    private static final long serialVersionUID = 1L;

    @Size(max = 30)
    @Column(name = "NOM_AGENT")
    private String nomAgent;

    @Size(max = 35)
    @Column(name = "PRENOM_AGENT")
    private String prenomAgent;

    @Id
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 30)
    @Column(name = "IDAGENT")
    private String idagent;

    @OneToMany(mappedBy = "idagent")
    private List<Intervention> interventionList;

    @JoinColumn(name = "ID_DEPT", referencedColumnName = "ID_DEPT")
    @ManyToOne
    private Departement idDept;

what could be the cause of the problem ?

The AgentBean :

    @Named(value = "agentBean")
     @SessionScoped    
     public class AgentBean implements Serializable {

     @EJB
     private Manager manager;

    private List <Agentministere> agentministeres;
    private Agentministere agentministere=new Agentministere();

    public AgentBean() {
    }

    public void setAgentministeres(List<Agentministere> agentministeres) {
        this.agentministeres = agentministeres;
    }


   public List<Agentministere> getAgentministeres() {
        agentministeres=manager.getAllAgents();
        return agentministeres;
    }

   public String list() {
        System.out.println("###LIST###");
        return "AgentList?faces-redirect=true";
    }

     public Agentministere getAgentministere() {
        return agentministere;
    }

    public void setAgentministere(Agentministere agentministere) {
        this.agentministere = agentministere;
    }

    public String enregistrerAgent()  {

   manager.agentCreer(agentministere);
    FacesContext context = FacesContext.getCurrentInstance();
    context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Succès", "Agent Creé avec succès !"));
    return "AgentList";
    }
    }

Session bean :

         //---------Agent----
public void agentCreer( Agentministere agentministere) {

agentministereFacade.create(agentministere);
   }
SRy
  • 2,901
  • 8
  • 36
  • 57
Ines Tunisie
  • 37
  • 1
  • 6

1 Answers1

1

Two things:

1) It's not a good ideia call a data base service to return some value in a get method:

public List<Agentministere> getAgentministeres() {
    agentministeres=manager.getAllAgents();
    return agentministeres;
}

The life cycle of JSF will call this method a few times...

You can try this: What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?

Make a init method in your bean, that prepares the bean to show the initial data.

2) The action "enregistrerAgent" in your mBean doesn't need to return a String. You can make it void, and after inser the new Agent in your database, just list all the agents again

agentministeres=manager.getAllAgents();

and update the list on the client:

<p:commandButton update=":formId"/>
Community
  • 1
  • 1