2013-05-28 66 views
0

我使用PrimeFaces selectOneMenu在它們旁邊顯示一些圖像和字符串,我只關注圖像旁邊的字符串,圖像本身僅用於顯示,我試過但它沒有工作:PrimeFaces selectOneMenu不渲染圖像和字符串

<p:selectOneMenu id="SkinChooser" 
     value="#{personBean.skin}" panelStyle="width:150px" 
       effect="fade" var="s" style="width:160px"> 
     <f:selectItem itemLabel="Select One" itemValue="" /> 
     <f:selectItems value="#{personBean.selectedSkins}" 
       var="skin" itemLabel="#{skin.skinType}" itemValue="#{skin}" /> 
     <p:column> 
       <p:graphicImage value="/resources/images/skin/#{s.skinPhoto}" /> 
     </p:column> 
     <p:column> 
       #{s.skinType} 
    </p:column> 
</p:selectOneMenu> 



    public class Skin { 
      String skinPhoto; 
      String skinType; 

     public Skin() {} 

     public Skin(String photo, String type) {} 

     public String getSkinPhoto() {return skinPhoto;} 

     public void setSkinPhoto(String skinPhoto) { 
       this.skinPhoto = skinPhoto; 
     } 

     public String getSkinType() { 
        return skinType; 
     } 

     public void setSkinType(String skinType) { 
        this.skinType = skinType; 
     } 
     @Override 
      public String toString() { 
          return skinType; 
      } 
     } 

personBean裏面我初始化ArrayList中selectedSkins如下:

,這是personBean:

@ManagedBean(name = "personBean") 
@SessionScoped 
public class ReportPerson { 
private Skin skin; 
private static List<Skin> selectedSkins; 


static { 
    System.err.println("Array is filled"); 
    selectedSkins = new ArrayList<Skin>(); 
    selectedSkins.add(new Skin("1", "Pale white")); 
    selectedSkins.add(new Skin("2", "Fair white")); 
    selectedSkins.add(new Skin("3", "Light brown")); 
    selectedSkins.add(new Skin("4", "Moderate brown")); 
    selectedSkins.add(new Skin("5", "Dark brown")); 
    selectedSkins.add(new Skin("6", "Deeply pigmented")); 
    System.err.println("Finished Filling"); 

} 

public List<Skin> getSelectedSkins() { 
    return selectedSkins; 
} 

public void setSelectedSkins(List<Skin> selectedSkins) { 
    this.selectedSkins = selectedSkins; 
} 

public Skin getSkin() { 
    return skin; 
} 

public void setSkin(Skin skin) { 
    this.skin = skin; 
     } 

    } 

但selectOneMenu組件仍然不渲染任何東西!

+0

請發佈您的託管bean的代碼。 –

+0

@Luiggi門多薩,編輯 –

回答

0

您缺少轉換器。

@ManagedBean(name = "skinConverter") 
public class SkinConverter implements Serializable, Converter { 

/** Serial Version UID. */ 
private static final long serialVersionUID = 3661819160508007879L; 

@ManagedProperty(value = "#{personBean}") 
private PersonBean personBean; 

/** 
* Accesses the personBean 
* @return the personBean 
*/ 
public final PersonBean getPersonBean() { 
    return personBean; 
} 

/** 
* Sets the personBean 
* @param personBean the personBean to set 
*/ 
public final void setPersonBean(final PersonBean personBean) { 
    this.personBean = personBean; 
} 

/** 
* @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext, 
*  javax.faces.component.UIComponent, java.lang.String) 
*/ 
@Override 
public Object getAsObject(final FacesContext facesContext, final UIComponent 
      component, final String submittedValue) { 
    if (submittedValue.trim().equals("")) { 
     return null; 
    } else { 
     for (Skin p : personBean.getSelectedSkins()) { 
      if (p.getSkinType().equals(submittedValue)) { 
       return p; 
      } 
     } 
    } 
    return null; 

} 

/** 
* @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext, 
*  javax.faces.component.UIComponent, java.lang.Object) 
*/ 
@Override 
public String getAsString(final FacesContext arg0, final UIComponent arg1, final Object value) { 
    if (value == null || value.equals("")) { 
     return ""; 
    } else { 
     return String.valueOf(((Skin) value).getSkinType()); 
    } 

} 

}

+0

OP的問題不在於提交表單,而在於表單的呈現。到目前爲止發佈的代碼並不需要轉換器。 – BalusC

+0

@BalusC你在哪裏看到問題? –

+0

@BalusC你是對的..轉換器不是解決方案。只有將itemValue設置爲#{skinvar}才能爲我工作。 –

0

爲了使項目作爲柱,標籤<f:selectItems>的屬性itemValue必須指向對象,而不是字符串。

就你而言,itemValue="#{skin}"是正確的。

如果您使用itemValue="skin"itemValue="#{skin.type}",則該項呈現爲普通文本,因爲其類型爲String

PrimeFaces源代碼將使其更清晰。

參考:SelectOneMenu.java