<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 6,  comments - 7,  trackbacks - 0
      2008年2月25日
     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 


        以上一段很簡單的代碼,很容易看懂。它的運行結果如下:
    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()的結果格式為類名@對象的16進制Hash表示。這里我們可以如此理解,是一個指向DataItem類實例化時,在內存中開辟的一塊空間的地址標識。
        在調用函數tr.newItem1()(24行)之前,item1和item2所指向的內存空間是相同的。所以在改變item1的同時item2的值勢必更這一起改變,同理改變item2的內容,item1的內容也會做出相同的改變。item1.toString()和item2.toString()的結果正可以說明這一點。這也說明了,item1和item2存儲的都是一個內存地址。
        當調用
    tr.newItem1(),重新實例化item1,之后item1指向的另一塊內存空間,而item2保持不變,指向最初那塊內存空間。此時,item1和和item2的內容將是毫不相關的。

    posted @ 2008-03-04 17:33 zhan 閱讀(1590) | 評論 (2)編輯 收藏

    1.       HTML代碼

    最終實現的效果代碼,如下所示:

    <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標簽:

    <h:selectOneMenu value="#{voutputType.DEVICETYPE}" converter="voutputDeviceTypeConverter">

    <f:selectItems value="#{voutput.deviceTypeList}"/>

    </h:selectOneMenu>

    主要有三個部分組成

    (a)     value="#{voutputType.DEVICETYPE}"

    javabean ,voutputType中的DEVICETYPE屬性,確定html代碼中<option selected="selected" value="Monitor">項的值

    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屬性對應的變量是枚舉DeviceType的一個實例。

    (b)    converter="voutputDeviceTypeConverter"

    類型轉換器,在在"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代碼如下:

    (實現< select><option>String類型值,與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對應變量必須是SelectItemjavax.faces.model.SelectItem)列表,所以有必要將DeviceType類型實例的值和對應String類型值,封裝在一個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()));

    }

    posted @ 2008-03-03 16:15 zhan 閱讀(2125) | 評論 (3)編輯 收藏

    使用JSF編寫web程序的時候,JavaBean無法直接通過相對路徑來訪問文件。經過一天的研究主要發現兩類解決方案,一是,通過FacesContext,二是,通過ClassLoader

    下面通過實例來說明。

    首先是介紹web程序目錄的大致結構:

    D:"......"Tomcat 6.0"webapps"imatrixb ------> 程序的更目錄

             --META-INF

             --WEB-INF

             ---------------classess

             ---------------------------cn

             ----------------------------------com

             --------------------------------------------……                        ----------->class 文件

             ---------------------------XmlData

             ---------------------------------path-config.xml           1

                      --------------- path-config.xml                                           2

    …….

    Index.jsp

    一:FacesContext

    獲得(2)號path-config.xml文件信息,

    代碼如下:

    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為相對路徑

             采用這個方法存在一些隱患:比方說對一個打包的應用來說,是沒有RealPath的概念的,調用getRealPath只會簡單地返回null

             2. getResourceAsStream():

    FacesContext context = FacesContext.getCurrentInstance();

    InputStream xmlStream = context.getExternalContext()

                  .getResourceAsStream(xmlfile);

        用于只讀的形式。

        二:ClassLoader

    獲得(1)號path-config.xml文件信息,

    代碼如下:

    String partPath =”/XmlData/path-config.xml”;

    String fullPath=this.getClass().getClassLoader().getResource(partPath).getPath();

    //使用的時候還是存在一些問題,無法正常使用,暫時沒有發現解決的辦法

    InputStream xmlStream=this.getClass().getClassLoader().getResourceAsStream(partPath);

             //用于只讀的形式下,通過測試能夠正常的使用

    posted @ 2008-02-29 17:36 zhan 閱讀(2100) | 評論 (2)編輯 收藏

    1.2 反射

    1.2.1 學習筆記

    參考資料:Java 2 核心技術I:基礎知識(第7版) 5.5 反射

    (1) Class

        在程序運行期間,Java運行時系統始終為所有對象的維護一個被稱為運行時的類型標識。這個信息保存著每一個對象所有屬性的類足跡。虛擬機利用運行信息選擇相應的方法執行。

    獲取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) 反射的分析能力

     示例:

    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"); // 返回名稱為“name”的私有或公有成員(域)

                       f.setAccessible(true); // 非常重要,否則無法調用f.get(zhanjh)方法

                       Object v = f.get(zhanjh);// 返回zhanjh對象中 name成員(域)的值

                       System.out.println(v.toString());

             }

    }

    /*

     * 運行結果: zhan jh

     */

    posted @ 2008-02-26 17:09 zhan 閱讀(156) | 評論 (0)編輯 收藏
         摘要: 從去年12月份還是學習Java到現在已經將近3個月了,現在已經很有必要對以前所學的知識進行一次系統的復習。而重新復習最好的辦法就是將最近剛完成,但不完善的Xml數據配置的Web程序,進行一次重構。 其中需要重新復習的知識主要內容如下: 1.             ...  閱讀全文
    posted @ 2008-02-25 17:09 zhan 閱讀(1105) | 評論 (0)編輯 收藏
    <2008年2月>
    272829303112
    3456789
    10111213141516
    17181920212223
    2425262728291
    2345678

    常用鏈接

    留言簿(1)

    隨筆檔案

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲国产综合在线| 国产又长又粗又爽免费视频| 久久亚洲av无码精品浪潮| 国产精品亚洲一区二区三区| 国产美女无遮挡免费网站| 亚洲av无码成人精品区一本二本| 成年美女黄网站色大免费视频| 中文字幕 亚洲 有码 在线| 免费无码又爽又刺激聊天APP| 亚洲乱码在线视频| 成年18网站免费视频网站| 亚洲国产精品成人综合色在线| 青青草国产免费久久久91| 白白色免费在线视频| 最新精品亚洲成a人在线观看| 成人免费乱码大片A毛片| 亚洲精品美女久久777777| 国产精品视频白浆免费视频| 久久精品国产亚洲av高清漫画| 精品免费久久久久久久| 亚洲国产欧美国产综合一区| www亚洲精品少妇裸乳一区二区| 亚洲免费无码在线| 亚洲日韩区在线电影| 成人毛片免费视频| 曰批全过程免费视频免费看| 亚洲精品成人网站在线观看| 久久爰www免费人成| 亚洲AV无码无限在线观看不卡| 国产大片91精品免费看3| 最近的2019免费中文字幕| 中文字幕亚洲精品| 青青青青青青久久久免费观看| aaa毛片免费观看| 亚洲精品成人网站在线播放| 国产美女无遮挡免费视频网站 | 一区二区视频免费观看| 亚洲人成电影福利在线播放| 日韩一级在线播放免费观看| 免费无码又爽又刺激高潮软件| 97se亚洲国产综合自在线|