2017-04-07 50 views
1

我遇到了問題p:selectOneMenu在JSF PrimeFaces。我讀了很多關於同樣問題的問題,但沒有任何幫助。Primefaces驗證錯誤與轉換器和等於工作

當我建立了我的部分我以前做的地方在我的項目,如果我嘗試選擇我selectOneMenu用於的項目之一同樣的方式,出現此錯誤:人

Validation Error: Value is not valid

地塊通過糾正轉換器類或等於()方法來解決此問題,但在礦中沒有任何錯誤。

轉換

@RequestScoped 
public class BaremeConverter implements Converter { 

    @EJB 
    private BaremeBean baremeBean; 

    @Override 
    public Object getAsObject(FacesContext fc, UIComponent uic, String value) { 
     if(value != null && value.trim().length() > 0) { 
      try { 
       return baremeBean.loadById(Integer.parseInt(value)); 
      } catch(NumberFormatException e) { 
       return null; 
      } 
     } 
     else { 
      return null; 
     } 
    } 

    @Override 
    public String getAsString(FacesContext fc, UIComponent uic, Object object) { 
     if(object != null) { 
      return String.valueOf(((Bareme) object).getId()); 
     } 
     else { 
      return null; 
     } 
    }  
} 

BaremeBean是這個類加載數據細的EntityBean的。我的工作空間裏充滿了這樣的轉換器,除非我錯過了這個東西,它應該在這裏工作。類Bareme

@Override 
public boolean equals(Object object) { 
    if (!(object instanceof Bareme)) { 
     return false; 
    } 
    Bareme other = (Bareme) object; 
    return !((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))); 
} 

這的

equals()方法是()方法生成的Netbeans通過等號並沒有什麼錯看起來也在這裏。

最後,我給你我使用的組件的代碼,以及與之前的組件相同的代碼,相同的代碼適用於其他類。

<h:outputLabel for="forfaitBareme" value="Barème" /> 
<p:selectOneMenu id="forfaitBareme" class="print-w100p-lab" value="#{transportFacturationBean.forfait.bareme}" converter="#{baremeConverter}" > 
    <f:selectItem itemLabel="" itemValue="#{null}" /> 
    <f:selectItems value="#{transportFacturationBean.baremesForfait}" var="b" itemLabel="#{b.id}" itemValue="#{b}" /> 
    <p:ajax event="change" update=":centralPanel" process="@form" /> 
</p:selectOneMenu> 

transportFacturationBean.baremesForfait是一個java.util.List含少量Bareme。

你應該知道使用我的項目的另一個自定義對象,下面的代碼運行良好。 Camion的實現方式與Bareme相同,它們的轉換器類似,它們的equals()方法都是由Netbeans生成的。

<h:outputLabel for="forfaitCamion" value="Camion" /> 
<p:selectOneMenu id="forfaitCamion" class="print-w100p-lab" value="#{transportFacturationBean.forfait.camion}" converter="#{camionConverter}" > 
    <f:selectItem itemLabel="" itemValue="#{null}" /> 
    <f:selectItems value="#{transportFacturationBean.camions}" var="c" itemLabel="#{c.type}" itemValue="#{c}" /> 
    <p:ajax event="change" update=":centralPanel" process="@form" /> 
</p:selectOneMenu> 

任何幫助,將不勝感激!提前致謝 !

+0

重複的http://stackoverflow.com/q/9069379描述了您未提及的第三個原因。 – BalusC

+0

@BalusC如果你正在討論的是選擇的項目可能不在可用的項目中,我的項目首先是空的,正如你所看到的,我添加了值爲null的f:selectItem,它適用於我的Camion對象。我還嘗試在我的ManagedBean的可用列表中選擇另一個項目,但錯誤仍在此處。 – rvz

+0

呃,我不是在說空項目。只是在實際處理表單提交時,所選項目不再在可用項目列表中。 – BalusC

回答

1

已解決! 最大最小錯誤我能想象!

return baremeBean.loadById(Integer.parseInt(value)); 

loadById法,而不是返回一個列表的簡單對象的....對不起你們!