Primefaces收集器删除不工作

时间:2013-04-25 06:11:36

标签: jsf-2 collections primefaces

我正在测试我的代码在Showcase中给出的primefaces收集器示例 我在某处读到了必须覆盖equals和hashcode方法。

即使覆盖了这些方法,我仍然会遇到同样的错误。

请告诉我我的代码中有什么错误

User.java

  @ManagedBean
  public class User implements Serializable{

        public String name;
        public String designation;
        public String division;

public User(String name,String division){
   setName(name);
   setDivision(division);
}
public User(){

}

public String getDesignation() {
    return designation;
}

public void setDesignation(String designation) {
    this.designation = designation;
}


public String getDivision() {
    return division;
}

public void setDivision(String userDivision) {
    this.division = userDivision;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

}

commApprover.java

@ManagedBean
@ViewScoped
public class CommApprover implements Serializable{

private User approver = new User();

private List<User> approvers = new ArrayList<User>();

public String reinit() {
    approver = new User();

    return null;
}

public User getApprover() {
    return approver;
}

public void setApprover(User approver) {
    this.approver = approver;
}

public List<User> getApprovers() {
    return approvers;
}

public void setApprovers(List<User> approvers) {
    this.approvers = approvers;
}

@Override
public boolean equals(Object o) 
{ 
    if (this == o) 
        return true; 
    if (o == null || getClass() != o.getClass()) 
        return false; 
     if (approver!= null ? !approver.equals(this.approver) : this.approver != null) 
         return false;

     return true; 
 }  

     @Override 
     public int hashCode() 
     { 
         int result = approver.hashCode(); 

         return result; 
     } 

}

的index.xhtml     

<p:growl id="msgs" />

<p:panel header="Approval Route ">
        <h:panelGrid columns="3" id="grid">
            <h:outputText  value="Name*" />
  <h:outputText value="Designation*" />
<h:outputText value="Division*" />

<p:inputText id="app_name" value="#{commApprover.approver.name}" required="true"/>
<p:inputText id="app_designation" value="#{commApprover.approver.designation}"     required="true"/>
<p:inputText id="app_division" required="true" value="#    {commApprover.approver.division}" />


<p:commandButton id="btn_add" value="Add" update="approvers @parent" action="#{commApprover.reinit}" >
<p:collector value="#{commApprover.approver}"  addTo="#{commApprover.approvers}" />
</p:commandButton>

</h:panelGrid>

</p:panel>

<p:outputPanel id="approvers">
<p:dataTable id="approversTable" value="#{commApprover.approvers}" var="approver">

<p:column>
<f:facet name="header">
    <h:outputText value="Name" />
</f:facet>
<h:outputText value="#{approver.name}" />
</p:column>

<p:column>
<f:facet name="header">
<h:outputText value="Designation" />
</f:facet>
<h:outputText value="#{approver.designation}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Division" />
</f:facet>
<h:outputText value="#{approver.division}" />
</p:column>

<p:column>
<f:facet name="header">
<h:outputText value="Operation" />
</f:facet>
<p:commandLink ajax="true" value="Remove" update=":appform:approvers" process=":appform:approvers">
<p:collector  value="#{approver}" removeFrom="#{commApprover.approvers}" />
</p:commandLink>
</p:column>
</p:dataTable>
</p:outputPanel>

   </h:form>

1 个答案:

答案 0 :(得分:0)

这篇帖子已经过时了,但我遇到了同样的问题,经过一些调整后我发现这个问题与错误实现 hashCode 等于有关,当使用转换器时或p:收集器您必须实现 hashCode 等于来比较实体中的所有字段,否则即使您尝试删除的项目是正确的项目,它也会失败。另外,建议您在Pojo中覆盖那些不在ManagedBean中的属性。这篇文章帮助我理解了问题https://blog.art-of-coding.eu/jsf-converters-and-equals-hashcode/