我搜索了这个着名的“验证错误:价值无效”。我知道它可能与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());
}
}
实际上如果我用这种方式编写equals方法:
@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 :(得分: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自动生成一个。