1.HTML代码
最终实现的效果代码,如下所示:
selected="selected"
value="Monitor">Monitor
value="VCR">VCR
Standard Device
Smart Device
value="Trunk">Trunk
Standby VCR
2.enum代码
publicenumDeviceType {
@XmlEnumValue("Monitor")
MONITOR("Monitor"),
VCR("VCR"),
@XmlEnumValue("Standard Device")
STANDARD_DEVICE("Standard Device"),
@XmlEnumValue("Smart Device")
SMART_DEVICE("Smart Device"),
@XmlEnumValue("Trunk")
TRUNK("Trunk"),
@XmlEnumValue("Standby VCR")
STANDBY_VCR("Standby VCR");
privatefinalStringvalue;
DeviceType(String v) {
value= v;
}
publicString value() {
returnvalue;
}
publicstaticDeviceType fromValue(String v)
{
for(DeviceType c: DeviceType.values()) {
if(c.value.equals(v)) {
returnc;
}
}
thrownewIllegalArgumentException(v);
}
}
3.JSF标签:
value="#{voutputType.DEVICETYPE}"
converter="voutputDeviceTypeConverter">
主要有三个部分组成
(a)value="#{voutputType.DEVICETYPE}"
由javabean,voutputType中的DEVICETYPE属性,确定html代码中
value="Monitor">项的值。
voutputType配置信息在"WebRoot"WEB-INF"faces-config.xml:
voutputType
cn.com.gentek.imatrix.xml.jaxb.voutput.ObjVOutputType
session
其中DEVICETYPE属性对应的变量是枚举DeviceType的一个实例。
(b)converter="voutputDeviceTypeConverter"
类型转换器,在在"WebRoot"WEB-INF"faces-config.xml配置如下:
voutputDeviceTypeConverter
cn.com.gentek.imatrix.ui.VoutDeviceTypeConverter
cn.com.gentek.imatrix.ui.VoutDeviceTypeConverter代码如下:
(实现< select>中的String类型值,与DeviceType类型之间的转换)
publicclassVoutDeviceTypeConverterimplementsConverter {
publicObject
getAsObject(FacesContext context, UIComponent component,String value) {
DeviceType result =null;
if(value ==null|| value.length() < 1) {
result =null;
}else
result = DeviceType.fromValue(value);
returnresult;
}
publicString
getAsString(FacesContext context, UIComponent component,Object value) {
String result =null;
if(value !=null) {
if(valueinstanceofDeviceType)
{
DeviceType temp =
(DeviceType) value;
result = temp.value();
}
}
returnresult;
}
}
(c)(重点)
由于deviceTypeList对应变量必须是SelectItem(javax.faces.model.SelectItem)列表,所以有必要将DeviceType类型实例的值和对应String类型值,封装在一个SelectItem实例中。实现代码如下:
ArrayList deviceTypeList =newArrayList();
for(inti = 0; i < DeviceType.values().length; i++) {
deviceTypeList.add(newSelectItem(DeviceType.values()[i],
DeviceType.values()[i].value()));
}
posted on 2008-03-03 16:15 zhan 阅读(2026) 评论(3) 编辑 收藏