2017-06-08 60 views
0

當遇到「驗證錯誤:值無效」錯誤時,我無法找到自己的錯誤。提交表單後,即使在調試時,即使得到此錯誤,選定的組件也是正確的。轉換器似乎也正常工作,這就是爲什麼我在這裏有一個非常艱難的時間。以下是我迄今爲止嘗試:JSF驗證錯誤:值無效

  • 變化@RequestScoped到@ManagedBean和@ViewScoped - 取得了SelectOneMenue beofore因爲一個NullPointerException的崩潰每次,這並不會與@RequestScoped發生,所以我不知道發生了什麼......

  • 我對此SelectedOneMenue檢查了轉換器,它似乎工作,我做了很多調試那裏,轉換爲字符串的對象,至少可見我,後來從String轉換回Object。

  • equals()方法:equals方法對我來說似乎可以,它實際上只是檢查.id是否相同,當在轉換器上檢查它時,它總是正確的。也許我寧願在這裏搜索錯誤,儘管我無法弄清楚這裏可能有什麼錯誤。

這裏是我的代碼,我猜會是你感興趣的部分,我已經取得了相當一些研究,我發現相當不錯的東西,雖然我不能得到這個工作,但這個一定不要吝嗇因爲我剛剛開始使用JavaEE和JSF(+ Primefaces)進行編程。

元器件equals()方法

@Override 
public boolean equals(Object obj) { 
    if(obj == null){ 
     //System.out.println("Component is NULL in equals() method"); 
    } 
    if(this.id == ((Component) obj).id) { 
     return true; 
    }else { 
     return false; 
    } 
} 

元器件轉換器類

@ManagedBean(name = "componentConverterBean") 
@FacesConverter (value = "componentConverter") 
public class ComponentConverter implements Converter{ 

@PersistenceContext(unitName="PU") 
private EntityManager em; 

@Override 
public String getAsString(FacesContext context, UIComponent component, Object modelValue) { 
    if (modelValue == null) { 
     return ""; 
    } 
    return modelValue.toString(); 
} 

@Override 
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) { 
    if (submittedValue == null || submittedValue.isEmpty()) { 
     System.out.println("ComponentConverter: submittedValue is null"); 
     return null; 
    } 

    try { 
     System.out.println("ComponentConverter: Value to be found: " + submittedValue); 
     return em.find(Component.class, Integer.parseInt(submittedValue)); 
    } catch (NumberFormatException e) { 
     throw new ConverterException(new FacesMessage(submittedValue + " is not a valid Component ID", e.getMessage())); 
    } 
} 

}

部分含有SelectOneMenues

<p:outputLabel for="facility" value="Facility: " /> 
<p:selectOneMenu id="facility" value="#{visualization.facility}" converter="#{facilityConverterBean}" style="width:150px"> 
    <p:ajax process="@this" partialSubmit="true" listener="#{visualization.onFacilityChange()}" update="component" /> 
    <f:selectItem itemLabel="Select Facility" noSelectionOption="true" /> 
    <f:selectItems value="#{visualization.facilities}" var="facility" itemLabel="#{facility.name}" itemValue="#{facility}" /> 
</p:selectOneMenu> 
<p:outputLabel for="component" value="Components: " /> 
<p:selectOneMenu id="component" value="#{visualization.component}" converter="#{componentConverterBean}" style="width:150px"> 
    <f:selectItem itemLabel="Select Component" noSelectionOption="true" /> 
    <f:selectItems value="#{visualization.components}" var="comp" itemLabel="#{comp.name}" itemValue="#{comp}" /> 
</p:selectOneMenu> 

「共同的.xhtml文件的mponents:Überprüfungsfehler:Wert istungültig。「 這是確切的錯誤,其轉換爲: 「組件:驗證錯誤:值無效。」

我非常感謝這裏的任何幫助。提前致謝。

+0

所以相關selectOneMenu用於工廠運作的?以及如何以及何時提交「組件」相關的一個,因爲只有當選定的「組件」發送到服務器 – Kukeltje

+0

可能重複https://stackoverflow.com/questions/9069379/validation-error-value-is-無效 – Tonkichi

+0

設施似乎工作,它幾乎相同的轉換器/等於方法,設施不給我一個錯誤。選擇完所有內容後,用戶將按提交,然後根據選定的設施,組件和從/到目前爲止我想從數據庫收集其他數據。這是否回答你的問題? –

回答

0

對於value =「#{visualization.com.com}」,使用容器對象而不是實體。 例如

class ComponentDto { 
    private Component componentEntity; 

    private Component getComponentEntity() { 
     return this.componentEntity; 
    } 

    private void setComponentEntity(Component componentEntity) { 
     this.componentEntity = componentEntity; 
    } 
} 

參見:https://github.com/primefaces/primefaces/issues/2756