我的问题是:
当 selectedPlantType 有默认值的时候, itemLabel 显示的是 "name" 而不是 "code". 请教高手如何处理?页面代码:
    <p:selectOneMenu value="#{plantBean.selectedPlantType}">
        <f:selectItems value="#{plantBean.plantTypes}" var="type" itemLabel="#{type.name}" itemValue="#{type.code}"/>
    </p:selectOneMenu>managedBean 代码:
    @ManagedBean(name = "plantBean")
    @ViewScoped
    public class PlantBean Serializable
    {
        ..........
        private PlantType selectedPlantType=new PlantType("name","code");
        private List<PlantType> plantTypes;
        ..........
        public PlantTree getSelectedPlantType()
        {
            return selectedPlantType;
        }
        public PlantTree setSelectedPlantType()
        {
            this.selectedPlantType=selectedPlantType;
        }
        public List<PlantType> getPlantTypes()
        {
            ........
            return plantTypes;
        }
    }PlantType class:  Code:
    public class PlantType implements Serializable
    {
        private String code;
        private String name;
        .........
        getter and setter methods
        .........
        equals and hashCode method
        .........
        @Override
        public String toString()
        {
            return name;
        }
    }the converter 代码:  Code:
    @FacesConverter(forClass=PlantType.class)
    public class PlantTypeConverter implements Converter
    {
        private static List<PlantType> types=.......   
        @Override
        public Object getAsObject(FacesContext context, UIComponent component, String value)
        {   
       if(null==value)
       {
           return null;
       }
       for(PlantType type:types) 
       {
           if(value.equals(type.getCode()))
           {
               return type;
           }
       } 
            return null;
        }        @Override
        public String getAsString(FacesContext context, UIComponent component, Object value)
        {
            return ((PlantType)value).getCode();
        }
    }PrimeFaces 2.2 RC2 , Mojarra 2.0.3, Glassfish3.0.1 netbeans 6.9.1

解决方案 »

  1.   

    struts 标签 if 判断!!!
      

  2.   

    页面代码: <h:selectOneMenu value="#{car.manufacturer}" >  
           <f:selectItems value="#{tableBean.manufacturers}" var="man" itemLabel="#{man}"   
                   itemValue="#{man}" />  
    </h:selectOneMenu> java代码: private final static String[] manufacturers;  
       setter/getter方法
         static {  
             manufacturers = new String[10];  
             manufacturers[0] = "Mercedes";  
             manufacturers[1] = "BMW";  
             manufacturers[2] = "Volvo";  
             manufacturers[3] = "Audi";  
            manufacturers[4] = "Renault";  
             manufacturers[5] = "Opel";  
             manufacturers[6] = "Volkswagen";  
             manufacturers[7] = "Chrysler";  
             manufacturers[8] = "Ferrari";  
             manufacturers[9] = "Ford";  
         } 不知道是不是你想要的...
      

  3.   

    分拿来。selectOneMenu里头的value=对象的关键属性,比如说是实体的主键。
    这样子这能达到你要的效果。而且免去converter的麻烦。注意的是selectedPlantType里头只有code属性是有效的。<p:selectOneMenu value="#{plantBean.selectedPlantType.code}" label="#{plantBean.selectedPlantType.name}">
      <f:selectItems value="#{plantBean.plantTypes}" var="type" itemLabel="#{type.name}" itemValue="#{type.code}"/>
      </p:selectOneMenu>