我的问题是:
当 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
当 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
解决方案 »
- 郁闷中谁来帮忙
- spring注入问题,急急!!在线等
- ${} 没明白它是怎么执行的,和 extremeponents 相比 struts 中的 ${} 各是怎么运行的?
- 一个js的date问题
- 为什么在velocity中使用递归不起作用呢?
- 郁闷整整2天了
- 好久没上来了,发现这里依然是这么热闹,高兴,散分!
- 哪位老大,可以醍醐灌顶的讲一下petstore中使用的关于event类,已经eventResponse类的细节,以及为什么要那样做。多谢了。
- 求struts中logic的使用方法及具体例子,谢谢
- 问题标签用户 急 处理大量数据 查找出重复的并去重(去重的导入另一张表中)
- hibernate及时加载与延迟加载问题,很纠结,高手赐教啊!
- hibernate引包错误
<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";
} 不知道是不是你想要的...
这样子这能达到你要的效果。而且免去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>