锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
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
浠ヤ笂涓孌靛緢綆鍗曠殑浠g爜錛屽緢瀹規槗鐪嬫噦銆傚畠鐨勮繍琛岀粨鏋滃涓嬶細
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()鐨勭粨鏋滄牸寮忎負綾誨悕@瀵硅薄鐨?6榪涘埗Hash琛ㄧず銆傝繖閲屾垜浠彲浠ュ姝ょ悊瑙o紝鏄竴涓寚鍚慏ataItem綾誨疄渚嬪寲鏃訛紝鍦ㄥ唴瀛樹腑寮杈熺殑涓鍧楃┖闂寸殑鍦板潃鏍囪瘑銆?br />
鍦ㄨ皟鐢ㄥ嚱鏁?span style="color: #000000;">tr.newItem1()錛?4琛岋級涔嬪墠錛?/span>item1鍜宨tem2鎵鎸囧悜鐨勫唴瀛樼┖闂存槸鐩稿悓鐨勩傛墍浠ュ湪鏀瑰彉item1鐨勫悓鏃秈tem2鐨勫煎娍蹇呮洿榪欎竴璧鋒敼鍙橈紝鍚岀悊鏀瑰彉item2鐨勫唴瀹癸紝item1鐨勫唴瀹逛篃浼氬仛鍑虹浉鍚岀殑鏀瑰彉銆?span style="color: #000000;">item1.toString()鍜?/span>item2.toString()鐨勭粨鏋滄鍙互璇存槑榪欎竴鐐廣傝繖涔熻鏄庝簡錛宨tem1鍜宨tem2瀛樺偍鐨勯兘鏄竴涓唴瀛樺湴鍧銆?br />
褰撹皟鐢?/span>tr.newItem1()錛岄噸鏂板疄渚嬪寲item1錛屼箣鍚巌tem1鎸囧悜鐨勫彟涓鍧楀唴瀛樼┖闂達紝鑰宨tem2淇濇寔涓嶅彉錛屾寚鍚戞渶鍒濋偅鍧楀唴瀛樼┖闂淬傛鏃訛紝item1鍜屽拰item2鐨勫唴瀹瑰皢鏄涓嶇浉鍏崇殑銆?/span>
]]>
鏈緇堝疄鐜扮殑鏁堟灉浠g爜錛屽涓嬫墍紺猴細
<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浠g爜
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鏍囩錛?/span>
<h:selectOneMenu value="#{voutputType.DEVICETYPE}" converter="voutputDeviceTypeConverter">
<f:selectItems value="#{voutput.deviceTypeList}"/>
</h:selectOneMenu>
涓昏鏈変笁涓儴鍒嗙粍鎴?/span>
(a) value="#{voutputType.DEVICETYPE}"
鐢?/span>javabean ,voutputType涓殑DEVICETYPE灞炴э紝紜畾html浠g爜涓?/span><option selected="selected" value="Monitor">欏圭殑鍊?/span>銆?/span>
voutputType閰嶇疆淇℃伅鍦?/span>"WebRoot"WEB-INF"faces-config.xml錛?/span>
<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灞炴у搴旂殑鍙橀噺鏄灇涓?/span>DeviceType鐨勪竴涓疄渚嬨?/span>
(b) converter="voutputDeviceTypeConverter"
綾誨瀷杞崲鍣紝鍦ㄥ湪"WebRoot"WEB-INF"faces-config.xml閰嶇疆濡備笅錛?/span>
<converter>
<converter-id>voutputDeviceTypeConverter</converter-id>
<converter-class>
cn.com.gentek.imatrix.ui.VoutDeviceTypeConverter
</converter-class>
</converter>
cn.com.gentek.imatrix.ui.VoutDeviceTypeConverter浠g爜濡備笅錛?/span>
錛堝疄鐜?/span>< select>涓?/span><option>鐨?/span>String綾誨瀷鍊鹼紝涓?/span>DeviceType綾誨瀷涔嬮棿鐨勮漿鎹級
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}"/>錛堥噸鐐癸級
鐢變簬deviceTypeList瀵瑰簲鍙橀噺蹇呴』鏄?/span>SelectItem錛?/span>javax.faces.model.SelectItem錛夊垪琛紝鎵浠ユ湁蹇呰灝?/span>DeviceType綾誨瀷瀹炰緥鐨勫煎拰瀵瑰簲String綾誨瀷鍊鹼紝灝佽鍦ㄤ竴涓?/span>SelectItem瀹炰緥涓傚疄鐜頒唬鐮佸涓嬶細
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()));
}
涓嬮潰閫氳繃瀹炰緥鏉ヨ鏄庛?/span>
棣栧厛鏄粙緇?/span>web紼嬪簭鐩綍鐨勫ぇ鑷寸粨鏋勶細
D:"......"Tomcat 6.0"webapps"imatrixb ------> 紼嬪簭鐨勬洿鐩綍
--META-INF
--WEB-INF
---------------classess
---------------------------cn
----------------------------------com
--------------------------------------------…… ----------->class 鏂囦歡
---------------------------XmlData
---------------------------------path-config.xml 錛?/span>1錛?/span>
--------------- path-config.xml 錛?/span>2錛?/span>
…….
Index.jsp
涓錛?/span>FacesContext
鑾峰緱錛?/span>2錛夊彿path-config.xml鏂囦歡淇℃伅,
浠g爜濡備笅錛?/span>
String partPath=”/ WEB-INF/ path-config.xml”;
1. getRealPath():
FacesContext
context = FacesContext.getCurrentInstance();
HttpServletRequest rst = (HttpServletRequest)context.getExternalContext().getRequest();
String fullPath=rst.getRealPath(xmlfile); // 鑾峰緱xml鏂囦歡鐨勭郴緇熻礬寰勶紝xmlfile涓虹浉瀵硅礬寰?/span>
閲囩敤榪欎釜鏂規硶瀛樺湪涓浜涢殣鎮o細姣旀柟璇村涓涓墦鍖呯殑搴旂敤鏉ヨ錛屾槸娌℃湁RealPath鐨勬蹇電殑錛岃皟鐢?/span>getRealPath鍙細綆鍗曞湴榪斿洖null銆?/span>
2. getResourceAsStream():
FacesContext context = FacesContext.getCurrentInstance();
InputStream xmlStream = context.getExternalContext()
.getResourceAsStream(xmlfile);
鐢ㄤ簬鍙鐨勫艦寮忋?/span>
浜岋細ClassLoader
鑾峰緱錛?/span>1錛夊彿path-config.xml鏂囦歡淇℃伅,
浠g爜濡備笅錛?/span>
String partPath =”/XmlData/path-config.xml”;
String fullPath=this.getClass().getClassLoader().getResource(partPath).getPath();
//浣跨敤鐨勬椂鍊欒繕鏄瓨鍦ㄤ竴浜涢棶棰橈紝鏃犳硶姝e父浣跨敤錛屾殏鏃舵病鏈夊彂鐜拌В鍐崇殑鍔炴硶
InputStream xmlStream=this.getClass().getClassLoader().getResourceAsStream(partPath);
//鐢ㄤ簬鍙鐨勫艦寮忎笅錛岄氳繃嫻嬭瘯鑳藉姝e父鐨勪嬌鐢?/span>
1.2.1 瀛︿範絎旇
鍙傝冭祫鏂欙細Java 2 鏍稿績鎶鏈?/span>鍗?/span>I錛氬熀紜鐭ヨ瘑錛堢7鐗堬級 5.5 鍙嶅皠
(1) Class綾?/span>
鍦ㄧ▼搴忚繍琛屾湡闂達紝Java榪愯鏃剁郴緇熷緇堜負鎵鏈夊璞$殑緇存姢涓涓縐頒負榪愯鏃剁殑綾誨瀷鏍囪瘑銆傝繖涓俊鎭繚瀛樼潃姣忎竴涓璞℃墍鏈夊睘鎬х殑綾昏凍榪廣傝櫄鎷熸満鍒╃敤榪愯淇℃伅閫夋嫨鐩稿簲鐨勬柟娉曟墽琛屻?/span>
鑾峰彇Class綾誨璞$殑涓夌鏂規硶
(a) getClass()
Employee e;
…
Class cl=e.getClass();
System.out.println(cl.getName()+“ ” +e.getName());
Result:
Employee Harry
(b) forName()
String className= “java.util.Date ”;
Class cl=Class.forName(className);
(c) .class
Class cl1=Date.class;
Class cl2=int.class;
(2) 鍙嶅皠鐨勫垎鏋愯兘鍔?/span>
紺轟緥錛?/span>
Employee.java:
publicclass Employee {
private String name;
privateintage;
public String getName() {
returnname;
}
publicvoid setName(String name) {
this.name = name;
}
publicint getSalary() {
returnage;
}
publicvoid setSalary(int salary) {
this.age = salary;
}
public Employee(String name, int salary) {
this.name = name;
this.age = salary;
}
}
Test.java
import java.lang.reflect.Field;
publicclass test {
publicstaticvoid main(String[] args) throws SecurityException,
NoSuchFieldException, IllegalArgumentException,
IllegalAccessException {
Employee zhanjh = new Employee("zhan jh", 1000);
Class<?> cl = zhanjh.getClass();
Field f = cl.getDeclaredField("name"); // 榪斿洖鍚嶇О涓?/span>“name”鐨勭鏈夋垨鍏湁鎴愬憳錛堝煙錛?/span>
f.setAccessible(true); // 闈炲父閲嶈錛屽惁鍒欐棤娉曡皟鐢?/span>f.get(zhanjh)鏂規硶
Object v = f.get(zhanjh);// 榪斿洖zhanjh瀵硅薄涓?/span> name鎴愬憳錛堝煙錛夌殑鍊?/span>
System.out.println(v.toString());
}
}
/*
* 榪愯緇撴灉錛?/span> zhan jh
*/