0

我正在测试 Showcase 中为我在某处读到的代码中给出的 primefaces 收集器示例,有必要为此重写 equals 和 hashcode 方法。

即使在覆盖方法之后,我仍然遇到同样的错误。

请告诉我我的代码有什么问题

用户.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; 
     } 

}

索引.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>
4

1 回答 1

0

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

于 2016-10-11T17:35:11.610 回答