2012-02-15 82 views
0

我搜索了這個著名的「驗證錯誤:值無效」了許多。我知道它可能與equals方法有關。但是找不到我的問題。著名的驗證錯誤:值無效

XHTML:

<h:selectOneMenu id="ayarMenuSA" rendered="#{teklifIslemleriBean.selectedITip == 1}" converter="metalConverter" value="#{teklifIslemleriBean.satisTeklif.metal}"> 
     <f:selectItems value="#{teklifIslemleriBean.ayarMenu}" /> 
</h:selectOneMenu> 

支持bean:

List<Metal> metalList = teklifIslemServisi.metalKodunaGoreMetalGetir(null, selectedMK);   
ayarMenu.clear(); 
for (Metal m : metalList) { 
    ayarMenu.add(new SelectItem(m, "Stok No:" + m.getMetalNo() + " ,Ayar: " + m.getAyar() + " ,Ağırlık: " + m.getAgirlik() + " ,MetalKod: " + m.getMetalKod().getMetalKodu())); 
} 

POJO:

public class Metal implements java.io.Serializable { 

    private Integer metalNo; 
    private MetalTip metalTip; 
    private MetalKod metalKod; 
    private BigDecimal agirlik; 
    private BigDecimal ayar; 
    private boolean durum; 

    public Metal() { 
    } 

    public Metal(boolean durum) { 
     this.durum = durum; 
    } 

    public Metal(MetalTip metalTip, MetalKod metalKod, BigDecimal agirlik, BigDecimal ayar, boolean durum) { 
     this.metalTip = metalTip; 
     this.metalKod = metalKod; 
     this.agirlik = agirlik; 
     this.ayar = ayar; 
     this.durum = durum; 
    } 


    public Integer getMetalNo() { 
     return this.metalNo; 
    } 

    public void setMetalNo(Integer metalNo) { 
     this.metalNo = metalNo; 
    } 

    public MetalTip getMetalTip() { 
     return this.metalTip; 
    } 

    public void setMetalTip(MetalTip metalTip) { 
     this.metalTip = metalTip; 
    } 

    public MetalKod getMetalKod() { 
     return this.metalKod; 
    } 

    public void setMetalKod(MetalKod metalKod) { 
     this.metalKod = metalKod; 
    } 

    public BigDecimal getAgirlik() { 
     return this.agirlik; 
    } 

    public void setAgirlik(BigDecimal agirlik) { 
     this.agirlik = agirlik; 
    } 

    public BigDecimal getAyar() { 
     return this.ayar; 
    } 

    public void setAyar(BigDecimal ayar) { 
     this.ayar = ayar; 
    } 

    public boolean isDurum() { 
     return this.durum; 
    } 

    public void setDurum(boolean durum) { 
     this.durum = durum; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (obj == null) { 
      return false; 
     } 
     if (getClass() != obj.getClass()) { 
      return false; 
     } 
     final Metal other = (Metal) obj; 
     if (this.metalNo != other.metalNo && (this.metalNo == null || !this.metalNo.equals(other.metalNo))) { 
      return false; 
     } 
     if (this.metalTip.getMetalTipiId() != other.metalTip.getMetalTipiId() && (this.metalTip == null || this.metalTip.getMetalTipiId() != (other.metalTip.getMetalTipiId()))) { 
      return false; 
     } 
     if (!this.metalKod.getMetalKodu().equals(other.metalKod.getMetalKodu()) && (this.metalKod == null || !this.metalKod.getMetalKodu().equals(other.metalKod.getMetalKodu()))) { 
      return false; 
     } 
     if (this.agirlik != other.agirlik && (this.agirlik == null || !this.agirlik.equals(other.agirlik))) { 
      return false; 
     } 
     if (this.ayar != other.ayar && (this.ayar == null || !this.ayar.equals(other.ayar))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 3; 
     hash = 17 * hash + (this.metalNo != null ? this.metalNo.hashCode() : 0); 
     hash = 17 * hash + (this.metalTip != null ? this.metalTip.hashCode() : 0); 
     hash = 17 * hash + (this.metalKod != null ? this.metalKod.hashCode() : 0); 
     hash = 17 * hash + (this.agirlik != null ? this.agirlik.hashCode() : 0); 
     hash = 17 * hash + (this.ayar != null ? this.ayar.hashCode() : 0); 
     return hash; 
    } 



} 

轉換器:

@FacesConverter("metalConverter") 
public class MetalConverter implements Converter, Serializable { 

    private static final long serialVersionUID = -1L; 


    @Override 
    public Object getAsObject(FacesContext facesContext, UIComponent component, String str) { 
     if (str == null || str.length() == 0) { 
      return null; 
     } 
     System.out.println("metalConverter getAsObject("+ str); 
     Metal metal = new Metal(); 
     String values[] = str.split(":"); 

     if (values.length > 1) { 
      metal.setMetalNo(Integer.parseInt(values[0])); 
      metal.setAyar(new BigDecimal(values[1])); 
      metal.setAgirlik(new BigDecimal(values[2]));//?? intcompact=1500 
      metal.setMetalKod(new MetalKod(values[3])); 
      metal.setMetalTip(new MetalTip(values[4])); 
     } 
     return metal; 
    } 

    Integer getKey(String value) { 
     Integer key; 
     key = Integer.valueOf(value); 
     return key; 
    } 

    String appendString(Integer value) { 
     StringBuilder sb = new StringBuilder(); 
     sb.append(value); 
     return sb.toString(); 
    } 

    @Override 
     public String getAsString(FacesContext facesContext, UIComponent component, Object object) { 

      if (object == null) { 
       return null; 
      } 
      if (object instanceof Metal) { 
       Metal metal = (Metal) object; 
       StringBuilder sb = new StringBuilder(); 
       sb.append(metal.getMetalNo()); 
       sb.append(':'); 
       sb.append(metal.getAyar().toString()); 
       sb.append(':'); 
       sb.append(metal.getAgirlik().toString()); 
       sb.append(':'); 
       sb.append(metal.getMetalKod().getMetalKodu()); 
       sb.append(':'); 
       sb.append(metal.getMetalTip().getMetalTipiId()); 
       System.out.println("metalConverter getAsString(object) -> " + sb.toString()); 
       return sb.toString(); 

      } else { 
       throw new IllegalArgumentException(object + " nesnesi " + object.getClass().getName() + "tipinde geliyor; olması gereken tip ise: " + Metal.class.getName()); 
      } 
     } 

其實如果我這樣寫等於方法:

@Override 
    public boolean equals(Object obj) { 
     if (obj == null) { 
      return false; 
     } 
     if (getClass() != obj.getClass()) { 
      return false; 
     } 
     final Metal other = (Metal) obj; 
     return true; 
    } 

它工作正常。但由於某些原因,我不得不使用上面的代碼。有沒有人對這個問題有任何想法?

回答

0

您不應該使用==!=來比較對象!=。你應該使用Object#equals()這個方法。例如Integer是一個對象,而int是一個原語,可以安全地使用==進行比較。

你需要重寫equals()方法如下:

private Integer metalNo; 
private MetalTip metalTip; 
private MetalKod metalKod; 
private BigDecimal agirlik; 
private BigDecimal ayar; 
private boolean durum; 

// ... 

public boolean equals(Object object) { 
    // Basic checks. 
    if (object == this) return true; 
    if (object == null || getClass() != object.getClass()) return false; 

    // Property checks. 
    Metal other = (Metal) object; 
    if (metalNo == null ? other.metalNo != null : !metalNo.equals(other.metalNo)) return false; 
    if (metalTip == null ? other.metalTip != null : !metalTip.equals(other.metalTip)) return false; 
    if (metalKod == null ? other.metalKod != null : !metalKod.equals(other.metalKod)) return false; 
    if (agirlik == null ? other.agirlik != null : !agirlik.equals(other.agirlik)) return false; 
    if (ayar == null ? other.ayar != null : !ayar.equals(other.ayar)) return false; 
    if (durum != other.durum) return false; 

    // All passed. 
    return true; 
} 

如果有必要,可以隨時讓你的IDE自動生成一個。

enter image description here

+0

有趣,因爲我用的Netbeans插入代碼選項生成此方法。我會實現你的答案並讓你知道結果。感謝您及時的回覆。 – lamostreta 2012-02-15 15:37:45

+0

仍然有相同的驗證錯誤。此代碼可能存在其他一些問題,但我無法找到它。 – lamostreta 2012-02-16 07:50:08

+0

也許'MetalTip'或'MetalKod'的'equals()'被破壞。 – BalusC 2012-02-16 11:43:53

相關問題