
2008年3月3日
1 package cn.com.gentek.imatrix.test;
2
3 public class tesRef {
4 private DataItem item1;
5 private DataItem item2;
6
7 public tesRef() {
8 item1 = new DataItem();
9 item2 = item1;
10 }
11
12 public void newItem1() {
13 item1 = new DataItem();
14 }
15
16 public void print() {
17 System.out.println("item1: " + item1.toString());
18 System.out.println("item2: " + item2.toString());
19 }
20
21 public static void main(String[] args) {
22 tesRef tr = new tesRef();
23 tr.print();
24 tr.newItem1();
25 tr.print();
26 }
27 }
28
以上一段很簡(jiǎn)單的代碼,很容易看懂。它的運(yùn)行結(jié)果如下:
item1: cn.com.gentek.imatrix.test.DataItem@c17164
item2: cn.com.gentek.imatrix.test.DataItem@c17164
item1: cn.com.gentek.imatrix.test.DataItem@1fb8ee3
item2: cn.com.gentek.imatrix.test.DataItem@c17164
toString()的結(jié)果格式為類名@對(duì)象的16進(jìn)制Hash表示。這里我們可以如此理解,是一個(gè)指向DataItem類實(shí)例化時(shí),在內(nèi)存中開(kāi)辟的一塊空間的地址標(biāo)識(shí)。
在調(diào)用函數(shù)
tr.newItem1()(24行)之前,item1和item2所指向的內(nèi)存空間是相同的。所以在改變item1的同時(shí)item2的值勢(shì)必更這一起改變,同理改變item2的內(nèi)容,item1的內(nèi)容也會(huì)做出相同的改變。
item1.toString()和item2.toString()的結(jié)果正可以說(shuō)明這一點(diǎn)。這也說(shuō)明了,item1和item2存儲(chǔ)的都是一個(gè)內(nèi)存地址。
當(dāng)調(diào)用tr.newItem1(),重新實(shí)例化item1,之后item1指向的另一塊內(nèi)存空間,而item2保持不變,指向最初那塊內(nèi)存空間。此時(shí),item1和和item2的內(nèi)容將是毫不相關(guān)的。
1.
HTML代碼
最終實(shí)現(xiàn)的效果代碼,如下所示:
<select>
<option
selected="selected"
value="Monitor">Monitor</option>
<option
value="VCR">VCR</option>
<option value="Standard
Device">Standard Device</option>
<option value="Smart
Device">Smart Device</option>
<option
value="Trunk">Trunk</option>
<option value="Standby
VCR">Standby VCR</option>
</select>
2.
enum代碼
publicenum DeviceType {
@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");
privatefinal String value;
DeviceType(String v) {
value = v;
}
public String value() {
returnvalue;
}
publicstatic DeviceType fromValue(String v)
{
for (DeviceType c: DeviceType.values()) {
if (c.value.equals(v)) {
return c;
}
}
thrownew IllegalArgumentException(v);
}
}
3.
JSF標(biāo)簽:
<h:selectOneMenu
value="#{voutputType.DEVICETYPE}"
converter="voutputDeviceTypeConverter">
<f:selectItems value="#{voutput.deviceTypeList}"/>
</h:selectOneMenu>
主要有三個(gè)部分組成
(a)
value="#{voutputType.DEVICETYPE}"
由javabean ,voutputType中的DEVICETYPE屬性,確定html代碼中<option selected="selected"
value="Monitor">項(xiàng)的值。
voutputType配置信息在"WebRoot"WEB-INF"faces-config.xml:
<managed-bean>
<managed-bean-name>voutputType</managed-bean-name>
<managed-bean-class>
cn.com.gentek.imatrix.xml.jaxb.voutput.ObjVOutputType
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
其中DEVICETYPE屬性對(duì)應(yīng)的變量是枚舉DeviceType的一個(gè)實(shí)例。
(b)
converter="voutputDeviceTypeConverter"
類型轉(zhuǎn)換器,在在"WebRoot"WEB-INF"faces-config.xml配置如下:
<converter>
<converter-id>voutputDeviceTypeConverter</converter-id>
<converter-class>
cn.com.gentek.imatrix.ui.VoutDeviceTypeConverter
</converter-class>
</converter>
cn.com.gentek.imatrix.ui.VoutDeviceTypeConverter代碼如下:
(實(shí)現(xiàn)< select>中<option>的String類型值,與DeviceType類型之間的轉(zhuǎn)換)
publicclass VoutDeviceTypeConverter implements Converter {
public Object
getAsObject(FacesContext context, UIComponent component, String value) {
DeviceType result = null;
if (value == null || value.length() < 1) {
result = null;
} else
result = DeviceType.fromValue(value);
returnresult;
}
public String
getAsString(FacesContext context, UIComponent component, Object value) {
String result = null;
if (value != null) {
if (value instanceof DeviceType)
{
DeviceType temp =
(DeviceType) value;
result = temp.value();
}
}
return result;
}
}
(c)
<f:selectItems value="#{voutput.deviceTypeList}"/>(重點(diǎn))
由于deviceTypeList對(duì)應(yīng)變量必須是SelectItem(javax.faces.model.SelectItem)列表,所以有必要將DeviceType類型實(shí)例的值和對(duì)應(yīng)String類型值,封裝在一個(gè)SelectItem實(shí)例中。實(shí)現(xiàn)代碼如下:
ArrayList<SelectItem> deviceTypeList = new ArrayList<SelectItem>();
for (int i = 0; i < DeviceType.values().length; i++) {
deviceTypeList.add(new
SelectItem(DeviceType.values()[i],
DeviceType.values()[i].value()));
}