posts - 6,  comments - 7,  trackbacks - 0
  2008年3月4日
 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 阅读(1575) | 评论 (2)编辑 收藏
  2008年3月3日

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 阅读(2115) | 评论 (3)编辑 收藏
  2008年2月29日

使用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 阅读(2085) | 评论 (2)编辑 收藏
  2008年2月26日

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 阅读(147) | 评论 (0)编辑 收藏
  2008年2月25日
     摘要: 从去年12月份还是学习Java到现在已经将近3个月了,现在已经很有必要对以前所学的知识进行一次系统的复习。而重新复习最好的办法就是将最近刚完成,但不完善的Xml数据配置的Web程序,进行一次重构。 其中需要重新复习的知识主要内容如下: 1.             ...  阅读全文
posted @ 2008-02-25 17:09 zhan 阅读(1092) | 评论 (0)编辑 收藏
  2007年12月26日
interfaces

上午完成thinking Java中关于Interfaces章节的内容。下面是该章节中关于"Interfaces and factories"的例子
package com.zhanjh.thinkingjava.interfaces;

interface Service{
    void method1();
    void method2();
}

interface ServiceFactory{
    Service getService();
}

class Implementation1 implements Service{
    public Implementation1() {
        // TODO Auto-generated constructor stub
    }
   
    public void method1(){
        System.out.println("Implementation1 method1");
    }
    public void method2(){
        System.out.println("Implementation1 method2");
    }
}

class Implementation1Factory implements ServiceFactory{
    public Service getService(){
        return new Implementation1();
    }
}

class Implementation2 implements Service{
    public Implementation2() {
        // TODO Auto-generated constructor stub
    }
   
    public void method1(){
        System.out.println("Implementation2 method1");
    }
    public void method2(){
        System.out.println("Implementation2 method2");
    }
}

class Implementation2Factory implements ServiceFactory{
    public Service getService(){
        return new Implementation2();
    }
}

public class Factories{
    public static void serviceConsumer(ServiceFactory fact){
        Service s=fact.getService();
        s.method1();
        s.method2();
    }
    public static void main(String[] args){
        serviceConsumer(new Implementation1Factory());
        serviceConsumer(new Implementation2Factory());
    }
}
总结:abstract class和interface是Java语言中对于抽象类定义进行支持的两种机制,abstract class和interface之间在对于抽象类定义的支持方面具有很大的相似性。目前我对他们区分的方法大致如下:
1)interface可以多重实现,而abstract class只能单一继承
2)abstract class不一定只有抽象的方法(abstract method),它也可以包含具体的方法(concrete method)。而interface不能包含方法的实现(implementation)。所以在程序设计的时候,能用inteface的时候尽量不要用abstract class。

下午
     查找关于EJB的资料,没头绪。
     jaxb入门学习。
     xjc(将xsd文件转换为Java的小工具)工具的使用。可以创建一个bat文件处理下面的命令:
    xjc -d "D:"eclipse"workspace"JaxbTest"src" -p "edu.jlu.xml" "D:"eclipse"workspace"JaxbTest"schema"messages.xsd"
其中D:"eclipse"workspace"JaxbTest"src为原文件的目录,edu.jlu.xml为生成Java类的包名,D:" eclipse"workspace"JaxbTest"schema"messages.xsd为xml schema文件的路径。

posted @ 2007-12-26 19:07 zhan 阅读(205) | 评论 (0)编辑 收藏
仅列出标题  
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(1)

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜