当我尝试在我的数据表上插入许多行时出现问题,显示数据有问题:在数据表上显示2次被插入的行
例如:
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>
实体
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;
问题可能是什么原因?
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";
}
}
会话bean:
//---------Agent----
public void agentCreer( Agentministere agentministere) {
agentministereFacade.create(agentministere);
}
答案 0 :(得分:1)
两件事:
1)在get方法中返回一些值并不是一个好的ideia调用数据库服务:
public List<Agentministere> getAgentministeres() {
agentministeres=manager.getAllAgents();
return agentministeres;
}
JSF的生命周期会调用此方法几次......
你可以试试这个: What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
在bean中创建一个init方法,准备bean以显示初始数据。
2) mBean中的“enregistrerAgent”操作不需要返回String。您可以将其设置为无效,并且在数据库中使用新代理后,只需再次列出所有代理
agentministeres=manager.getAllAgents();
并更新客户端上的列表:
<p:commandButton update=":formId"/>