简体   繁体   English

PrimeFaces selectOneMenu不呈现图像和字符串

[英]PrimeFaces selectOneMenu not rendering Images and Strings

I'm using PrimeFaces selectOneMenu to display some images and strings next to them, i'm only concerned with the string next to the image, the image itself is for displaying only, i tried this but it didn't work: 我正在使用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;
             }
       }

inside the bean personBean i initialized the ArrayList selectedSkins as follows: 在bean personBean内部,我按如下方式初始化ArrayList selectedSkins

and this is the personBean: 这是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;
        }

    }

but the selectOneMenu component still doesn't render anything! 但是selectOneMenu组件仍然无法呈现任何内容!

You are missing the converter. 您缺少转换器。

@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());
    }

}

} }

In order to render an item as column, the attribute itemValue of the tag <f:selectItems> must point to an Object, not a String. 为了将项目呈现为列,标记<f:selectItems>的属性itemValue必须指向对象,而不是字符串。

In your case, itemValue="#{skin}" is correct. 您的情况是itemValue="#{skin}"是正确的。

If you use itemValue="skin" or itemValue="#{skin.type}" , the item is rendered as normal text because its type is String . 如果您使用itemValue="skin"itemValue="#{skin.type}" ,则该项目将显示为普通文本,因为其类型为String

The PrimeFaces source code will make it clearer. PrimeFaces源代码将使其更加清晰。

Reference: SelectOneMenu.java 参考: SelectOneMenu.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM