2017-08-15 129 views
0

我想從使用PrimeFaces 5.3和JSF 2的selectCheckboxMenu中獲取選定值,但總是選擇列表爲空。selectCheckboxMenu primefaces返回空列表

這是HTML部分:

<p:selectCheckboxMenu id="cours" value="#{etudiantController.checkedCours}" 
    converter="#{coursConverter}" label="Liste cours available" multiple="true" 
    panelStyle="width:250px"> 
    <f:selectItems value="#{etudiantController.coursEtudiant}" var="coursEtd" 
     itemLabel="#{coursEtd.libelleCours}" itemValue="#{coursEtd.idCours}" /> 
</p:selectCheckboxMenu> 

豆部分:

private List<Cours> checkedCours; 

for(Cours coursToAdd : checkedCours){ 
    System.out.println("enter ... !!!"); 
    coursService.addCours(coursToAdd); 
} 

轉換器:

public class CoursConverter implements Converter { 

public Object getAsObject(FacesContext context, UIComponent component, String value) { 
    if (value != null && !value.isEmpty()) { 
     return component.getAttributes().get(value); 
    } 
    return null; 
} 

public String getAsString(FacesContext context, UIComponent component, Object value) { 
    if (value == null) { 
     return ""; 
    } 
    if (value instanceof Cours) { 
     Cours cours = (Cours) value; 
     String name = cours.getLibelleCours(); 
     return name; 
    } else { 
     throw new ConverterException(new FacesMessage(value + " est un Cours non valide")); 
    } 
} } 

這裏的問題是,它從來沒有在塊進入。

任何幫助? 謝謝。

+0

當你得到所選擇的值?在表單提交上,對嗎? –

+0

是的,使用提交。 – Spoonatte

+0

你有沒有添加表單標籤到你的xhtml? –

回答

0

嘗試的ID設置爲你的轉換器,並使用該ID

@FacesConverter("coursConverter") 
public class CoursConverter implements Converter { 
    ... 
} 

叫它 -

<p:selectCheckboxMenu id="cours" value="#{etudiantController.checkedCours}" 
    converter="coursConverter" label="Liste cours available" multiple="true" 
    panelStyle="width:250px"> 
    <f:selectItems value="#{etudiantController.coursEtudiant}" var="coursEtd" 
    itemLabel="#{coursEtd.libelleCours}" itemValue="#{coursEtd.idCours}" />