Vincent

          Vicent's blog
          隨筆 - 74, 文章 - 0, 評論 - 5, 引用 - 0
          數據加載中……

          2006年9月18日

          一、 橋梁(Bridge)模式

               摘要: 一、?橋梁(Bridge)模式 橋梁模式是一個非常有用的模式,也是比較復雜的一個模式。熟悉這個模式對于理解面向對象的設計原則,包括"開-閉"原則(OCP)以及組合/聚合復用原則(CARP)都很有幫助。理解好這兩個原則,有助于形成正確的設計思想和培養良好的設計風格。 注:《Java與模式》一書認為Bridge模式不是一個使用頻率很高的模式,我不太贊同,我認為Bridge模式中...  閱讀全文

          posted @ 2006-09-18 13:38 Binary 閱讀(3290) | 評論 (2)編輯 收藏

          2006年9月1日

          Hibernate Validator 簡介

          在項目的業務屬性中,你是不是要經常驗證屬性的取值范圍呢. 想要了解比較優美的解決方案嗎???????????

          看看Hibernate Validator 是怎么做的吧.一見到她,相信你就會說: Oh God, 這就是我需要的.

          任何獲得Matrix授權的網站,轉載請保留以下作者信息和鏈接:
          作者:icess(作者的blog:http://blog.matrix.org.cn/page/icess)
          關鍵字:Hibernate Validator

          用Annotations 給類或者類的屬性加上約束(constraint),在運行期檢查屬性值是很優雅的.Hibernate Validator就是這樣的一個框架.該框架是十分容易的(就像參考文檔中宣稱的那樣),幾乎沒有什么學習曲線,Validator 是一個驗證框架 不需要和Hibernate的其他部分綁定就可以使用,只要在你的項目中添加Hibernate-annotations.jar庫就可以了.那么下面就讓我們看看怎么使用吧.

          Person.java 類

          /*
          ? *?Created?on?2006-1-12 Person.java
          ? *?@author?
          ? */
          package? test.annotation.validator;

          import? org.hibernate.validator.Length;
          import? org.hibernate.validator.Min;
          import? org.hibernate.validator.Valid;
           

          //@Serializability? //測試自定義約束
          public?class? Person?{

          ?? private? String?name;
          ?? private?int? age;
          ?? private? Address?address;
          ??
          ?? public? Person()?{}
          ??
          ?? @Valid //注意此處
          ?? public? Address?getAddress()?{
          ???? return? address;
          ?? }
          ?? public?void? setAddress(Address?address)?{
          ???? this .address?=?address;
          ?? }
          ??
          ?? @Min(value?=? 1 )
          ?? public?int? getAge()?{
          ???? return? age;
          ?? }
          ?? public?void? setAge( int? age)?{
          ???? this .age?=?age;
          ?? }
          ??
          ?? @Length(min?=? 4 )
          ?? public? String?getName()?{
          ???? return? name;
          ?? }
          ?? public?void? setName(String?name)?{
          ???? this .name?=?name;
          ?? }
          }

           

          Address.java 類

          /*
          ? *?Created?on?2006-1-12 Address.java
          ? *?@author?
          ? */
          package? test.annotation.validator;

          import? org.hibernate.validator.Length;
          import? org.hibernate.validator.Max;
          import? org.hibernate.validator.Min;

          public?class? Address?{

          ?? private? String?street;
          ?? private?int? num;
          ??
          ?? public? Address()?{}
          ??
          ?? @Min(value?=? 1 )
          ?? @Max(value?=? 100 )
          ?? public?int? getNum()?{
          ???? return? num;
          ?? }
          ?? public?void? setNum( int? num)?{
          ???? this .num?=?num;
          ?? }
          ??
          ?? @Length(min?=? 3 ,max?=? 8 )
          ?? public? String?getStreet()?{
          ???? return? street;
          ?? }
          ?? public?void? setStreet(String?street)?{
          ???? this .street?=?street;
          ?? }
          }

          上面是兩個用 Validator Annotations 注釋的 類. 每個屬性都用 約束限制了.? 下面看看測試的類吧:

          TestValidator.java 類

          /*
          ? *?Created?on?2006-1-12
          ? *?@author?icerain
          ? */
          package? test.annotation.validator;

          import? org.hibernate.validator.ClassValidator;
          import? org.hibernate.validator.InvalidValue;


          public?class? TestValidator?{
          ?? public?void? test()?{
          ???? Address?add?=? new? Address();
          ???? add.setNum( 0 );
          ???? add.setStreet( "1" );
          ????
          ???? Person?p?=? new? Person();
          ???? p.setAddress(add);
          ???? p.setAge( 0 );
          ???? p.setName( "ice" );
          ????
          ???? /******************Test?validator?********/

          ??? // 注意該處只驗證了Person 為了說明 @Valid 注釋的使用
          ???? ClassValidator<Person>?classValidator?=? new? ClassValidator<Person>?(Person. class );
          ???? InvalidValue[]?validMessages?=?classValidator.getInvalidValues(p);
          ???? for? (InvalidValue?value?:?validMessages)?{
          ??????
          ???? System.out.println( "InvalidValue?的長度是:"? +?validMessages.length
          ???????? + "?.?驗證消息是:?"? +?value.getMessage()?
          ???????? +? "?.?PropertyPath?是:"? +?value.getPropertyPath()
          ???????? + "?.\n\t?PropertyName?是:?"? +value.getPropertyName()
          ???????? +? "Value?是:?"? +?value.getValue()
          ???????? + "?Bean?是:?" +?value.getBean()
          ???????? + "\n\t?BeanClass?是:"? +?value.getBeanClass());
          ???? }
          ?? }
          ??
          ?? public?static?void? main(String[]?args)?{
          ???? new? TestValidator().test();
          ?? }
          }

           

          程序的輸出如下

          InvalidValue 的長度是:4 . 驗證消息是: 必須大于等于 1 . PropertyPath 是:age .

          PropertyName 是: age. Value 是: 0 Bean 是: test.annotation.validator.Person@dd87b2

          BeanClass 是:class test.annotation.validator.Person

          InvalidValue 的長度是:4 . 驗證消息是: 長度必須介于 4 與 2147483647 之間 . PropertyPath 是:name .

          PropertyName 是: name. Value 是: ice Bean 是: test.annotation.validator.Person@dd87b2

          BeanClass 是:class test.annotation.validator.Person

          InvalidValue 的長度是:4 . 驗證消息是: 必須大于等于 1 . PropertyPath 是:address.num .

          PropertyName 是: num. Value 是: 0 Bean 是: test.annotation.validator.Address@197d257

          BeanClass 是:class test.annotation.validator.Address

          InvalidValue 的長度是:4 . 驗證消息是: 長度必須介于 3 與 8 之間 . PropertyPath 是:address.street .

          PropertyName 是: street. Value 是: 1 Bean 是: test.annotation.validator.Address@197d257

          BeanClass 是:class test.annotation.validator.Address

          可以看出不滿足約束的值都被指出了.

          同時該句: ClassValidator<Person>?classValidator?=?new?ClassValidator<Person>?(Person.class);

          我們只驗證了 Person. 在Person里面的Address的屬性 由于有@Valid Annotations 所以 Address的相關屬性也被機聯驗證了 .

          如果 把@Valid Annotations 去掉,結果如下:

          InvalidValue 的長度是:2 . 驗證消息是: 必須大于等于 1 . PropertyPath 是:age .

          PropertyName 是: age. Value 是: 0 Bean 是: test.annotation.validator.Person@18fef3d

          BeanClass 是:class test.annotation.validator.Person

          InvalidValue 的長度是:2 . 驗證消息是: 長度必須介于 4 與 2147483647 之間 . PropertyPath 是:name .

          PropertyName 是: name. Value 是: ice Bean 是: test.annotation.validator.Person@18fef3d

          BeanClass 是:class test.annotation.validator.Person

          可以看出 沒有驗證 Address.

          當然了 ,你還可以只驗證一個屬性 , 沒有必要驗證整個類.只需要在調用 classValidator.getInvalidValues(p,"age")方法時 加上你要驗證的屬性就可以了.如我們只想驗證age 屬性 把代碼改為如下所示:

          InvalidValue[] validMessages = classValidator.getInvalidValues(p,"age"); / /只驗證age 屬性

          運行結果如下:

          InvalidValue 的長度是:1 . 驗證消息是: 必須大于等于 1 . PropertyPath 是:age .

          PropertyName 是: age. Value 是: 0 Bean 是: test.annotation.validator.Person@1457cb

          BeanClass 是:class test.annotation.validator.Person

          只是驗證了 age 屬性.

          怎么樣 ,很簡單吧. 關于 Hibernate Validator 內建的驗證Annotations 大家可以看看 API 或者 參考文檔(中文版我正在翻譯中 請訪問我的 Blog 獲得最新信息).

          如果你要寫自己的約束呢 , 你不用擔心 ,這也是很容易的. 任何約束有兩部分組成: [約束描述符 即注釋]the constraint descriptor (the annotation) 和[約束validator 即 實現類] the constraint validator (the implementation class).下面我們擴展Hibernate Test suit 中的一個Test 來講解一下.

          首先: 要聲明一個 constraint descriptor .如下:

          package? test.annotation.validator;

          import? java.lang.annotation.Documented;
          import?static? java.lang.annotation.ElementType.TYPE;
          import?static? java.lang.annotation.ElementType.FIELD;
          import?static? java.lang.annotation.ElementType.METHOD;
          import? java.lang.annotation.Retention;
          import?static? java.lang.annotation.RetentionPolicy.RUNTIME;
          import? java.lang.annotation.Target;

          import? org.hibernate.validator.ValidatorClass;

          /**
          ? *?Dummy?sample?of?a?bean-level?validation?annotation
          ? *
          ? *? @author? Emmanuel?Bernard
          ? */
          @ValidatorClass(SerializabilityValidator. class )
          @Target({METHOD,FIELD,TYPE})
          @Retention(RUNTIME)
          @Documented
          public? @interface?Serializability?{
          ?? int? num()? default? 11 ;
          ?? String?message()? default? "bean?must?be?serialiable" ;
          }

          @ValidatorClass(SerializabilityValidator. class ) 指出了 constraint validator 類.

          @Target({METHOD,FIELD,TYPE})
          @Retention(RUNTIME)
          @Documented????????????????

          這幾個我就不用解釋了吧.

          Serializability?里面聲明了一個 message 顯示約束的提示信息. num 只是為了說明一個方面 在這里面沒有實際用途用 .

          然后就是 實現一個 constraint validator 類 該類要實現Validator<ConstraintAnnotation>.這里是SerializabilityValidator.java 如下:

          //$Id:?SerializabilityValidator.java,v?1.3?2005/11/17?18:12:11?epbernard?Exp?$
          package? test.annotation.validator;

          import? java.io.Serializable;

          import? org.hibernate.validator.Validator;

          /**
          ? *?Sample?of?a?bean-level?validator
          ? *
          ? *? @author? Emmanuel?Bernard
          ? */
          public?class? SerializabilityValidator? implements? Validator<Serializability>,?Serializable?{
          ?? public?boolean? isValid(Object?value)?{
          ??? //這里只是Validator 里面的 實現驗證規則的 方法. value 是要驗證的值.
          ???? System.out.println( "IN?SerializabilityValidator?isValid:" +value.getClass()+ ":?"? +value.toString());
          ???? return? value?instanceof?Serializable;
          ??}

          ??public?void?initialize(Serializability?parameters)?{
          ????//?在這里可以 取得
          constraint descriptor 里面的屬性 如上面我們聲明的 num
          ???? System.out.println( "IN?SerializabilityValidator:?parameters:" +?parameters.num()?);
          ?? }
          }

          然后在你的類中應用@Serializability? 就可以約束一個類實現Serializable 接口了. 如下:

          在我們的Person.java類 添加@Serializability? Annotations ,把Person.java 中的 //@Serializability //測試自定義約束 注釋去掉就ok了.

          運行結果如下:

          InvalidValue 的長度是:3 . 驗證消息是: bean must be serialiable . PropertyPath 是:null .

          PropertyName 是: null. Value 是: test.annotation.validator.Person@1a73d3c Bean 是: test.annotation.validator.Person@1a73d3c

          BeanClass 是:class test.annotation.validator.Person

          現在把Person類實現 java.io.Serializable 接口 則沒有出現 驗證錯誤消息.

          消息的國際化也是很簡單的,把 Serializability? 中的message 改為以{}擴住的 屬性文件的Key就可以了

          public? @interface?Serializability?{
          ?? int? num()? default? 11 ;
          ?? String?message()? default? "{Serializable}"; //"bean?must?be?serialiable"; //消息的國際化
          }

          然后編輯資料文件. 注意 該資源文件中要包括 Hibernate Validator 內建的資源. 可以在該org\hibernate\validator\resources 包里面的資源文件基礎上修改 ,在打包里面 這樣就可以了. 自己打包可能不太方便.你可以把該包里面的文件復制出來.然后放到你自己的項目包下在自己編輯, 該測試中 我是放在 test\resources 包下的.

          然后在 資源文件中添加 Serializable = '''''' 這么一行, 樣例如下:

          #DefaultValidatorMessages.properties (DefaultValidatorMessages_zh.properties 不再列出^_^)

           

          #下面是 Hibernate Validator 內建的國際化消息

          validator.assertFalse= assertion failed

          validator.assertTrue= assertion failed

          validator.future= must be a future date

          validator.length= length must be between {min} and {max}

          validator.max= must be less than or equal to {value}

          validator.min= must be greater than or equal to {value}

          validator.notNull= may not be null

          validator.past= must be a past date

          validator.pattern= must match "{regex}"

          validator.range= must be between {min} and {max}

          validator.size= size must be between {min} and {max}

          #下面是自定義的消息

          Serializable= Bean not Serializable? //加上自己定義的國際化消息.

          在構造 ClassValidator 時要添上 資源文件 如下:(在測試類中)

          ClassValidator<Person> classValidator = new ClassValidator<Person> (Person.class,ResourceBundle.getBundle("test.resources.DefaultValidatorMessages"));//加載資源

          這樣就可以了 .? 當然 你還可以 更改 Hibernate Validator 的消息(不是在上面的資源文件中直接修改 validator.length = ... 等等 ) , 還記得 Validator 注釋中有個 message 元素嗎? 你以前用的都是默認值,現在你可以該為你自己定義的了. 如:validator.length 我把他改為 "該字符串的長度不符合規定范圍范圍". 在資源文件中添加一行鍵值屬性對(key定義為 "myMsg")如下:

          myMsg=該字符串的長度不符合規定范圍范圍

          并且還要在 @Length 注釋中提供message的引用的key 如下 @Length(min = 4,message = "{ myMsg }")

          再一次運行測試 ,我們就可以看到上面兩條自定義綁定的消息了 .如下:

          InvalidValue 的長度是:3 . 驗證消息是: Bean 不是 可 Serializable . PropertyPath 是:null .
          PropertyName 是: null. Value 是: test.annotation.validator.Person@1bd4722 Bean 是: test.annotation.validator.Person@1bd4722
          BeanClass 是:class test.annotation.validator.Person


          InvalidValue 的長度是:3 . 驗證消息是: 該字符串的長度不符合規定范圍范圍 . PropertyPath 是:name .
          PropertyName 是: name. Value 是: ice Bean 是: test.annotation.validator.Person@1bd4722
          BeanClass 是:class test.annotation.validator.Person

          怎么樣,比你想象的簡單吧.

          OK 上面我們討論了 Hibernate Validator 的主要用法: 但是 該框架有什么用呢? ^_^

          看到這里其實不用我在多說了 大家都知道怎么用,什么時候用. 作為一篇介紹性文章我還是在此給出一個最常用的例子吧,更好的使用方式大家慢慢挖掘吧.

          比如 : 你現在在開發一個人力資源(HR)系統 (其實是我們ERP課程的一個作業 ^_^), 里面要處理大量的數據,尤其是在輸入各種資料時 如 登記員工信息. 如果你公司的員工的年齡要求是18 -- 60 那么你所輸入的年齡就不能超出這個范圍. 你可能會說這很容易啊 , 不用Validator就可以解決啊.這保持數據前驗證就可以啦 如if ( e.getAge() > 60 || e.getAge() < 18 ) ........ 給出錯誤信息 然后提示重新輸入不就OK啦 用得著 興師動眾的來個第三方框架嗎?

          是啊 當就驗證這一個屬性時, 沒有必要啊 ! 但是一個真正的HR 系統,會只有一個屬性要驗證嗎? 恐怕要有N多吧

          你要是每一個都那樣 寫一段驗證代碼 是不是很煩啊 ,況且也不方便代碼重用. 現在考慮一些 Validator 是不是更高效啊,攔截到 約束違例的 屬性 就可以直接得到 國際化的消息 可以把該消息顯示到一個彈出對話框上 提示更正? !

          Validator的用處不只這一種 ,你可以想到如何用呢 ! 歡迎發表你的高見!!

          OK 到此 我們的 Hibernate Validator 之旅就要先告一段落了 . 希望這是令你心曠神怡的一次寒冬之旅,

          把你學到的應用到你的項目中吧,一定會提高你的生產率的. 相信我 ,沒錯的? ^_^ !

          posted @ 2006-09-01 14:05 Binary 閱讀(460) | 評論 (0)編輯 收藏

          Hibernate Annotations 實戰(二)

          -- hbm.xml 與 Annotations 性能比較

          任何獲得Matrix授權的網站,轉載請保留以下作者信息和鏈接:
          作者:icess(作者的blog:http://blog.matrix.org.cn/page/icess)
          關鍵字:Hibernate Validator

          我在前面一篇文章<Hibernate Annotations 實戰-- 從 hbm.xml 到 Annotations>:

          中,有很多開發者在談論中提到,有沒有必要從 hbm.xml 往 Annotations 上轉移. 那么在這篇文章中我們就來討論一下 hbm.xml 與 Annotations的優缺點,看看那種情況最適合你.

          首先,討論一下 xml 配置文件的優點, 個人認為主要優點就是當你改變底層配置時 不需要改變和重新編譯代碼,只需要在xml 中更改就可以了,例如 Hibernate.cfg.xml 當你要更改底層數據庫時, 只要更改配置文件就可以了.Hibernate會為你做好別的事情.

          那么xml的缺點呢,個人認為有以下幾點:

          • 描述符多,不容易記憶,掌握 要深入了解還有看DTD文件

          • 無法做自動校驗,需要人工查找

          • 讀取和解析xml配置要消耗一定時間,導致應用啟動慢,不便于測試和維護

          • 當系統很大時,大量的xml文件難以管理

          • 運行中保存xml配置需要消耗額外的內存

          • 在O/R Mapping的時候需要在java文件和xml配置文件之間交替,增大了工作量

          其中第一 二點 借助于先進的IDE 可能不是什么問題. 但是對初學者還是個問題 ^_^.

           

          下面我們看看 Annotations的 特性吧! 可以解決xml遇到的問題,有以下優點

          • 描述符減少。以前在xml配置中往往需要描述java屬性的類型,關系等等。而元數據本身就是java語言,從而省略了大量的描述符

          • 編譯期校驗。錯誤的批注在編譯期間就會報錯。

          • 元數據批注在java代碼中,避免了額外的文件維護工作

          • 元數據被編譯成java bytecode,消耗的內存少,讀取也很快,利于測試和維護

          關于 映射文件是使用 hbm.xml 文件還是使用 Annotations 我們來看看2者的性能吧. 先聲明一下,個人認為映射文件一旦配置好就不會在很大程度上改變了.所以使用xml文件并不會帶來很大的好處.如果你認為 映射文件在你的項目中也經常變化,比如一列String數據 ,今天你使用 length="16" 明天你認為 該數據的長度應該更長才能滿足業務需求 于是改為length="128" 等等類似的問題 . 如果你經常有這方面的變動的話,下面的比較你可以不用看了 , 你應該使用 xml文件 因為Annotations 無法很好的滿足你的要求.

          現在讓我們就來看看2者的性能比較吧.

          (說明: 這里只是比較查找 插入 的時間快慢,沒有比較除運行時間以外的其他性能,如 內存占用量 等等)

          先來看看測試程序和配置.

          首先在 Hibernate.cfg.xml 文件中去掉了

          <property name="hibernate.hbm2ddl.auto">update</property>

          這一行, 因為在前面的實驗中以及建立了數據庫表了 不再需要更新了.如果你是第一次運行該例子 還是要該行的.

          Test.java 如下:

          /*
          ?*?Created?on?2005
          ?*?@author?
          ?*/
          package?test.hibernate.annotation;

          import?org.hibernate.Session;
          import?org.hibernate.Transaction;

          public?class?Test?{
          ??
          ??public?static?void?main(String?[]?args)?{
          ????long?start?=?0;
          ????long?end?=?0;
          ????start?=?System.currentTimeMillis();??//程序開始時間
          ????
          ????Session?s?=?HibernateUtil.currentSession();
          ????long?mid?=??System.currentTimeMillis();??//初始化完畢的時間 (可能此時并沒有初始化完畢^_^)
          ????
          ????Transaction?tx?=?s.beginTransaction();????
          ????/********************測試讀取的代碼************************/
          ????Person?p?=?null;
          ????for(int?i?=?1;?i?<=?100;?i?++)?{
          ????p?=?(Person)?s.get(Person.class,?i);
          ????System.out.println(p.getName());
          ????}
          ????System.out.println(p.getName());

          ??? /********************測試讀取1次的代碼************************/
          ????Person?p?=?null;
          ????p?=?(Person)?s.get(Person.class,?1);
          ????System.out.println(p.getName());
          ????/*********************測試插入的代碼*************************************/
          ????/*
          ????for?(int?i?=?0;?i?<?100;?i?++)?{
          ??????Person?p?=?new?Person();
          ??????p.setAge(i+1);
          ??????p.setName("icerain"+i);
          ??????p.setSex("male"+i);
          ??????s.save(p);
          ??????s.flush();
          ????}
          ????*/
          ????tx.commit();
          ????HibernateUtil.closeSession();
          ????
          ????end?=?System.currentTimeMillis();?//測試結束時間
          ????System.out.println("String[]?-?start?time:?"?+?start);
          ????System.out.println("String[]?-?end?time:?"?+?end);
          ????System.out.println("Init?time?:?"?+?(mid-start)); // 打印初始化用的時間
          ????System.out.println("Last?time?is?:"?+(end?-?mid)?); //打印 數據操作的時間
          ????System.out.println("Total?time?:?"?+(end?-?start)); //打印總時間
          ?
          ?}
          }

          Annotations 包中的Person.java 如下

          package?test.hibernate.annotation;

          import?java.util.LinkedList;
          import?java.util.List;

          import?javax.persistence.AccessType;
          import?javax.persistence.Basic;
          import?javax.persistence.Entity;
          import?javax.persistence.GeneratorType;
          import?javax.persistence.Id;
          import?javax.persistence.Table;
          import?javax.persistence.Transient;

          /**
          ?*?Person?generated?by?hbm2java
          ?*/

          @SuppressWarnings("serial")
          @Entity(access?=?AccessType.PROPERTY)
          @Table
          public?class?Person?implements?java.io.Serializable?{
          ??private?Integer?id;
          ??private?String?name;
          ??private?String?sex;
          ??private?Integer?age;
          ??private?List?list?=?new?LinkedList();

          ??//?Constructors
          ??/**?default?constructor?*/
          ??public?Person()?{
          ??}

          ??/**?constructor?with?id?*/
          ??public?Person(Integer?id)?{
          ????this.id?=?id;
          ??}

          ??//?Property?accessors
          ??@Id(generate=GeneratorType.AUTO)
          ??public?Integer?getId()?{
          ????return?this.id;
          ??}

          ??public?void?setId(Integer?id)?{
          ????this.id?=?id;
          ??}

          ??@Basic
          ??public?String?getName()?{
          ????return?this.name;
          ??}

          ??public?void?setName(String?name)?{
          ????this.name?=?name;
          ??}

          ??@Basic
          ??public?String?getSex()?{
          ????return?this.sex;
          ??}

          ??public?void?setSex(String?sex)?{
          ????this.sex?=?sex;
          ??}

          ??@Basic
          ??public?Integer?getAge()?{
          ????return?this.age;
          ??}

          ??public?void?setAge(Integer?age)?{
          ????this.age?=?age;
          ??}
          ??@Transient
          ??public?List?getList()?{
          ????return?list;
          ??}
          ??public?void?setList(List?list)?{
          ????this.list?=?list;
          ??}

          }

          其他的代碼幾乎沒有改變:

          下面的每種類型的測試都測試了3次以上, 取中間的測試時間.

          測試機器配置:

          CPU:? AMD Athlon (xp) 2000+

          內存: 784880KB

          硬盤: 三星 SP0812N

          讀取一次??的比較:(單位: 毫秒)

          使用Annotations 的測試數據使用Xml文件的測試數據簡要說明
          Init time : 2444Init time : 2431測試前我認為該項結果xml應該比較大,要讀取映射文件啊,實際情況不是這樣,不知道為什么?
          Last time is :62Last time is :85相差比較大不知道為什么?
          Total time : 2506Total time : 2516xml文件總體上慢了一點

          ?? 讀取100次的比較:

          使用Annotations 的測試數據使用Xml文件的測試數據簡要說明
          Init time : 2437Init time : 2422和前面初始化差不多
          Last time is :438Last time is :484有時間差
          Total time : 2875Total time : 2906也是xml文件總體上慢了一點

          插入100次的比較:

          使用Annotations 的測試數據使用Xml文件的測試數據簡要說明
          Init time : 2453Init time : 2469和前面初始化差不多
          Last time is :469Last time is :656有時間差
          Total time : 2922Total time : 3125也是xml文件總體上慢了一點

          從上面的三次對比中大家可以看到 初始化的部分幾乎兩者是一樣的, 在數據操作上面 使用xml文件 總是比使用Annotations 慢一點.在我們只操縱一個只有幾個屬性的小持久化類的操作中就有 幾十毫秒的差距. 幾十毫秒在計算機中算不算很大 大家應該都知道,我就不在多說了.

          總結: 經過 xml 文件 和Annotations 的優缺點和 性能上的對比.現在使用那個作為你持久化映射策略.我相信大家都會正確選擇的.

          測試后記: 經過多次測試 感覺有時候很不穩定 ,有的時候很穩定不知道是測試有問題還是別的問題.大家可以自己測試一下. 有什么新的發現 請大家討論討論.

          posted @ 2006-09-01 14:04 Binary 閱讀(343) | 評論 (0)編輯 收藏

          第一個Hibernate with Annotation程式

          Hibernate是ORM的解決方案,其底層對數據庫的操作依賴于JDBC,所以您必須先取得JDBC驅動程序,在這邊所使用的是MySQL,所以您必須至 MySQL? Connector/J 取得MySQL的JDBC驅動程序。

          接下來至
          Hibernate 官方網站 取得Hibernate 3.2Hibernate Annotations 3.2

          您必須安裝JDK 5.0才可以使用Hibernate Annotations的功能。

          解開Hibernate 3.2的zip檔案后,當中的hibernate3.jar是必要的,而在lib目錄中還包括了許多jar檔案,您可以在 Hibernate 3.0官方的參考手冊 上找到這些jar的相關說明,其中必要的是 antlr、dom4j、CGLIB、asm、Commons Collections、Commons Logging、 EHCache,Hibernate底層還需要Java Transaction API,所以您還需要jta.jar。

          解開Hibernate Annotations 3.2的zip檔案后,您需要hibernate-annotations.jar、ejb3-persistence.jar這兩個檔案。

          到這邊為止,總共需要以下的jar檔案:


          Hibernate可以運行于單機之上,也可以運行于Web應用程序之中,如果是運行于單機,則將所有用到的jar檔案(包括JDBC驅動程序)設定至CLASSPATH中,如果是運行于Web應用程序中,則將jar檔案置放于WEB-INF/lib中。

          如果您還需要額外的Library,再依需求加入,例如JUnit、Proxool等等,接下來可以將etc目錄下的 log4j.properties復制至Hibernate項目的Classpath下,并修改一下當中的 log4j.logger.org.hibernate為error,也就是只在在錯誤發生時顯示必要的訊息。

          接著設置基本的Hibernate配置文件,可以使用XML或Properties檔案,這邊先使用XML,檔名預設為hibernate.cfg.xml:

          <?xml version="1.0" encoding="utf-8"?>
          <!DOCTYPE hibernate-configuration PUBLIC
          ? "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          ? "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
          ?
          <hibernate-configuration>
          ??? <session-factory>
          ??????? <!-- 顯示實際操作數據庫時的SQL -->
          ??????? <property name="show_sql">true</property>
          ??????? <!-- SQL方言,這邊設定的是MySQL -->
          ??????? <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
          ??????? <!-- JDBC驅動程序 -->
          ??????? <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
          ??????? <!-- JDBC URL -->
          ??????? <property name="connection.url">jdbc:mysql://localhost/demo</property>
          ??????? <!-- 數據庫使用者 -->
          ??????? <property name="connection.username">root</property>
          ??????? <!-- 數據庫密碼 -->
          ??????? <property name="connection.password">123456</property>
          ?
          ??????? <!-- 以下設置對象與數據庫表格映像類別 -->
          ??????? <mapping class="onlyfun.caterpillar.User"/>
          ??? </session-factory>
          </hibernate-configuration>

          這邊以一個簡單的單機程序來示范Hibernate的配置與功能,首先作數據庫的準備工作,在MySQL中新增一個demo數據庫,并建立user表格:
          CREATE TABLE user (
          id INT(11) NOT NULL auto_increment PRIMARY KEY,
          name VARCHAR(100) NOT NULL default'',
          age INT
          );
          對于這個表格,您有一個User類別與之對應,表格中的每一個字段將對應至User實例上的Field成員。

          package onlyfun.caterpillar;
          ?
          import javax.persistence.*;
          ?
          @Entity
          @Table(name="user") // 非必要,在表格名稱與類別名稱不同時使用
          public class User {
          ? @Id
          ? @GeneratedValue(strategy=GenerationType.AUTO)
          ??? private Integer id;
          ?
          ? @Column(name="name") // 非必要,在字段名稱與屬性名稱不同時使用
          ??? private String name;
          ?
          ? @Column(name="age")
          ??? private Integer age; // 非必要,在字段名稱與屬性名稱不同時使用
          ???
          ??? // 必須要有一個預設的建構方法
          ??? // 以使得Hibernate可以使用Constructor.newInstance()建立對象
          ??? public User() {
          ??? }
          ?
          ??? public Integer getId() {
          ??????? return id;
          ??? }
          ?
          ??? public void setId(Integer id) {
          ??????? this.id = id;
          ??? }
          ?
          ??? public String getName() {
          ??????? return name;
          ??? }
          ?
          ??? public void setName(String name) {
          ??????? this.name = name;
          ??? }
          ???
          ??? public Integer getAge() {
          ??????? return age;
          ??? }
          ?
          ??? public void setAge(Integer age) {
          ??????? this.age = age;
          ??? }
          }

          其中id是個特殊的屬性,Hibernate會使用它來作為主鍵識別,您可以定義主鍵產生的方式,這邊設定為自動產生主鍵,可以看到,實體標識,主鍵生成,以及相關映像,都可以使用Annotation來完成。

          接下來撰寫一個測試的程序,這個程序直接以Java程序設計人員熟悉的語法方式來操作對象,而實際上也直接完成對數據庫的操作,程序將會將一筆數據存入表格之中:
          package onlyfun.caterpillar;
          ?
          import org.hibernate.SessionFactory;
          import org.hibernate.Session;
          import org.hibernate.Transaction;
          import org.hibernate.cfg.AnnotationConfiguration;
          import org.hibernate.cfg.Configuration;
          ?
          public class HibernateAnnotationDemo {
          ?
          ??? public static void main(String[] args) {
          ??????? // 需要AnnotationConfiguration讀取Annotation訊息
          ??????? Configuration config = new AnnotationConfiguration().configure();
          ??????? // 根據 config 建立 SessionFactory
          ??????? // SessionFactory 將用于建立 Session
          ??????? SessionFactory sessionFactory = config.buildSessionFactory();
          ?
          ??????? // 將持久化的物件
          ??????? User user = new User();
          ??????? user.setName("caterpillar");
          ??????? user.setAge(new Integer(30));????
          ?
          ??????? // 開啟Session,相當于開啟JDBC的Connection
          ??????? Session session = sessionFactory.openSession();
          ??????? // Transaction表示一組會話操作
          ??????? Transaction tx= session.beginTransaction();
          ??????? // 將對象映像至數據庫表格中儲存
          ??????? session.save(user);
          ??????? tx.commit();
          ??????? session.close();
          ??????? sessionFactory.close();
          ??????
          ??????? System.out.println("新增資料OK!請先用MySQL觀看結果!");
          ??? }
          }

          注意,使用Annotation時,需要的是AnnotationConfiguration類別。

          如您所看到的,程序中只需要直接操作User對象,并進行Session與Transaction的相關操作,Hibernate就會自動完成對數據庫的操作,您看不到任何一行JDBC或SQL的陳述,撰寫好以上的各個檔案之后,各檔案的放置位置如下:


          接著可以開始運行程序,結果如下:
          Hibernate: insert into user (name, age) values (?, ?)
          新增資料OK!請先用MySQL觀看結果!

          執行結果中顯示了Hibernate所實際使用的SQL,由于這個程序還沒有查詢功能,所以要進入MySQL中看看新增的數據,如下:
          mysql> select * from user;
          +----+-----------------+------+
          | id??? | name???????? | age? |
          +----+-----------------+------+
          |? 1??? | caterpillar? | 30?? |
          +----+-----------------+------+
          1 row in set (0.03 sec)

          posted @ 2006-09-01 14:00 Binary 閱讀(306) | 評論 (0)編輯 收藏

          Hibernate Annotations 實戰

               摘要: 任何獲得Matrix授權的網站,轉載請保留以下作者信息和鏈接: 作者:icess(作者的blog:http://blog.matrix.org.cn/page/icess)關鍵字:Hibernate Validator 下面讓我們先看一個通常用 hbm.xml 映射文件的例子. 有3個類 .HibernateUtil.java 也就是 Hibernate文檔中推薦的工具類,Pers...  閱讀全文

          posted @ 2006-09-01 13:59 Binary 閱讀(411) | 評論 (0)編輯 收藏

          在filter中關閉session

          利用Thread-Specific Storage撰寫一個HibernateUtil

          HibernateSessionUtil.java
          								import java.io.Serializable;

          import net.sf.hibernate.HibernateException;
          import net.sf.hibernate.Session;
          import net.sf.hibernate.SessionFactory;
          import net.sf.hibernate.Transaction;

          public class HibernateSessionUtil implements Serializable
          {
          publicstaticfinal ThreadLocal tLocalsess = new ThreadLocal();

          publicstaticfinal ThreadLocal tLocaltx = new ThreadLocal();

          /*
          * getting the thread-safe session for using
          */
          publicstatic Session currentSession(){
          Session session = (Session) tLocalsess.get();

          //open a new one, if none can be found.
          try{
          if (session == null){
          session = openSession();
          tLocalsess.set(session);
          }
          }catch (HibernateException e){
          thrownew InfrastructureException(e);
          }
          return session;
          }

          /*
          * closing the thread-safe session
          */
          publicstatic void closeSession(){

          Session session = (Session) tLocalsess.get();
          tLocalsess.set(null);
          try{
          if (session != null && session.isOpen()){
          session.close();
          }

          }catch (HibernateException e){
          thrownew InfrastructureException(e);
          }
          }

          /*
          * begin the transaction
          */
          publicstatic void beginTransaction(){
          Transaction tx = (Transaction) tLocaltx.get();
          try{
          if (tx == null){
          tx = currentSession().beginTransaction();
          tLocaltx.set(tx);
          }
          }catch (HibernateException e){
          thrownew InfrastructureException(e);
          }
          }

          /*
          * close the transaction
          */
          publicstatic void commitTransaction(){
          Transaction tx = (Transaction) tLocaltx.get();
          try{
          if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
          tx.commit();
          tLocaltx.set(null);
          }catch (HibernateException e){
          thrownew InfrastructureException(e);
          }
          }

          /*
          * for rollbacking
          */
          publicstatic void rollbackTransaction(){
          Transaction tx = (Transaction) tLocaltx.get();
          try{
          tLocaltx.set(null);
          if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()){
          tx.rollback();
          }
          }catch (HibernateException e){
          thrownew InfrastructureException(e);
          }
          }

          privatestatic Session openSession() throws HibernateException{
          return getSessionFactory().openSession();
          }

          privatestatic SessionFactory getSessionFactory() throws HibernateException{
          return SingletonSessionFactory.getInstance();
          }
          }

           filter中的程式碼如下

          HibernateSessionCloser.java
          								public class HibernateSessionCloser implements Filter{

          protected FilterConfig filterConfig = null;

          public void init(FilterConfig filterConfig)throws ServletException{
          this.filterConfig = filterConfig;
          }

          public void destroy(){
          this.filterConfig = null;
          }

          public void doFilter(ServletRequest request, ServletResponse response,
          FilterChain chain)
          throws IOException, ServletException {
          try{
          chain.doFilter(request, response);
          }
          finally{
          try{
          HibernateSessionUtil.commitTransaction();
          }catch (InfrastructureException e){
          HibernateSessionUtil.rollbackTransaction();
          }finally{
          HibernateSessionUtil.closeSession();
          }
          }

          }
          }

          然後在操作資料庫之前加上

          HibernateSessionUtil.beginTransaction();
          HibernateSessionUtil.currentSession();//取得Session

          posted @ 2006-09-01 13:51 Binary 閱讀(472) | 評論 (0)編輯 收藏

          acegi-security-sample-contacts-filter例子學習(二)

          功能實現分析

          這個例子使用了HSQL做數據庫,spring的AOP作為基礎,使用Acegi做安全控制組件。
          聯系人管理的web應用在啟動時候,會做一系列初始化動作:
          1. 讀取web.xml文件,

          2. 并解析文件里的內容。
          a) context-param元素。
          i. contextConfigLocation屬性。這個屬性定義了spring所需要的3個屬性文件。它們分別是:applicationContext -acegi-security.xml、applicationContext-common-business.xml、 applicationContext-common-authorization.xml
          ii. log4jConfigLocation屬性。這個屬性定義了log4j配置文件。

          b) filter元素。
          這里定義了acegi的一個過濾器。Acegi的大部分過濾器都是這樣配置的。使用FilterToBeanProxy組件,給它傳遞一個targetClass屬性。這個targetClass必須實現javax.servlet.Filter接口。
          這里配置的是FilterChainProxy。這個FilterChainProxy比較好用,可以為它定義一串filter屬性。這些filter將會按照定義的順序被調用。例如,
          <bean id="filterChainProxy" class="net.sf.acegisecurity.util.FilterChainProxy">
          <property name="filterInvocationDefinitionSource">
          <value>
          CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
          PATTERN_TYPE_APACHE_ANT
          /**=httpSessionContextIntegrationFilter,authenticationProcessingFilter,basicProcessingFilter,rememberMeProcessingFilter,anonymousProcessingFilter,securityEnforcementFilter
          </value>
          </property>
          </bean>
          這個過濾器的mapping是“/*”。
          c) listener元素。
          i. ContextLoaderListener。這個是Spring使用來加載根applicationcontext。并分別解析 applicationContext-acegi-security.xml、applicationContext-common- business.xml、applicationContext-common-authorization.xml等配置文件,把相關的對象初始化
          iii. Log4jConfigListener。這個是spring用來初始化log4j組件的listener。
          iv. HttpSessionEventPublisher。這個組件將發布HttpSessionCreatedEvent和HttpSessionDestroyedEvent事件給spring的applicationcontext。
          d) servlet元素。
          i. contacts。這里采用了spring的MVC框架, 所以這個servlet是spring MVC的一個核心控制器(org.springframework.web.servlet.DispatcherServlet)。這個servlet 啟動時候,會從contacts-servlet.xml里面讀取信息,并做相關的初始化。
          v. remoting。也是spring MVC的一個核心控制器。與contacts不同,這個servlet主要是提供web services服務。這個servlet啟動時候, 會從remoting-servlet.xml里面讀取信息,并做相關的初始化。
          e) taglib元素。這里定義了spring的標f) 簽庫。
          3. 解析applicationContext-acegi-security.xml。
          a) 過濾器鏈。定義了一個FilterChainProxy,b) 并指c) 定了一系列的過濾器鏈。httpSessionContextIntegrationFilter, authenticationProcessingFilter,basicProcessingFilter,rememberMeProcessingFilter,anonymousProcessingFilter,securityEnforcementFilter
          d) 認證管理器。這個管理器由acegi提供。這個管理器需要一個providers參數。這個providers參數包含了提供系統認證的對象。
          i. daoAuthenticationProvider。一般用戶認證。
          ii. anonymousAuthenticationProvider。匿名用戶認證。
          iv. rememberMeAuthenticationProvider。記住我認證。

          e) 密碼加密。這里定義了一個acegi的Md5算法加密對象Md5PasswordEncoder。
          f) 定義了一個jdbcDao實現類。這個類由acegi提供的net.sf.acegisecurity.providers.dao.jdbc.JdbcDaoImpl。這個對象需要一個dataSource的參數。
          g) 定義daoAuthenticationProvider。這個對象由acegi提供。它有3個屬性:
          authenticationDao。這里指向前面定義的jdbcDao。
          userCache。這里指向后面定義的user緩存對象。
          passwordEncoder。這里指向前面定義的密碼加密對象。
          h) 用戶緩存管理。
          為了緩存user,這里使用spring的ehcache來緩存user。緩存機制:
          i. 定義緩存管理器――CacheManager。這個對象是spring的EhCacheManagerFactoryBean對象
          ii. 定義user緩存實際執行對象――UserCacheBackend。這個對象是spring的EhCacheFactoryBean。它有兩個屬性:
          1. cacheManager。這里指向前面定義的緩存管理器。
          2. cacheName。
          iii. 定義user緩存――UserCache。它是acegi提供的EhCacheBasedUserCache對象。它有一個屬性:
          1. cache。這里指向的是前面定義的userCacheBackend。

          i) 定義接收來自DaoAuthenticationProvider的認證事件的listener――LoggerListener。
          j)
          4. 解析applicationContext-common-business.xml。
          a) dataSource.
          這里使用了spring的DriverManagerDataSource對象。這個對象是一個JDBC數據源的定義。
          b) TransactionManager。這里使用spring的DataSourceTransactionManager對象。
          c) 事務攔截器。這里使用spring的事務攔截器TransactionInterceptor。它有2個屬性:
          transactionManager。這個屬性指向前面定義的TransactionManager。
          transactionAttributeSource。這個屬性里, 指定了ContactManager的各個方法的事務方面的要求。
          d) DataSourcePopulator。
          使用sample.contact.DataSourcePopulator對象,往HSQL里創建相關的表結構和數據。
          實現原理:DataSourcePopulator 實現了接口 InitializingBean。其中afterPropertiesSet方法將在spring初始化DataSourcePopulator后被調用。
          e) ContactDao。這里指向一個ContactDaoSpring對象。它繼承spring的 JdbcDaoSupport,g) 并實現ContactDao接口。它是真正實現JDBC操作的對象。
          h) ContactManager。這里使用的是spring的ProxyFactoryBean。它有2個屬性:
          i. ProxyInterfaces。代理接口:sample.contact.ContactManager

          ii. InterceptorNames。攔截器名稱。可以有多個,iv. 這里包括:transactionInterceptor、contactManagerSecurity、contactManagerTarget。其中,v. transactionInterceptor是前面定義的事務攔截器。ContactManagerSecurity則是在 applicationContext-common-authorization.xml里定義的方法調用授權。
          i) ContactManagerTarget。這里指向的是sample.contact.ContactManagerBackend對象。 ContactManagerBackend實現了ContactManager接口和InitializingBean接口。它有2個自定義屬性: contactDao和basicAclExtendedDao。這里會調用ACL的API去做些創建權限和刪除權限的工作。

          posted @ 2006-09-01 13:45 Binary 閱讀(894) | 評論 (0)編輯 收藏

          acegi-security-sample-contacts-filter例子學習(一)

          這是一個 Acegi 官方的例子。它以聯系人的管理為例子,說明如何使用 Acegi 作權限控制。這個例子包含在 acegi 的包里面。下載地址: http://prdownloads.sourceforge.net/acegisecurity/acegi-security-0.8.3.zip?download

          聯系人管理說明了下列中心的Acegi安全控制能力:

          • Role-based security (基于角色的安全) ――每個責任人都是某個角色的一員。而角色被用來限制對某些安全對象的訪問。
          • Domain object instance security (域對象實例安全) ――合同,這個系統里的主要域對象,擁有一個訪問控制列表( ACL ),用來指明誰允許讀、管理和刪除對象。
          • Method invocation security (方法調用安全)―― 這個 ContactManager 服務層對象 包含一些受保護的和公開的方法。
          • Web request security Web 請求安全) ――這個“ /secure URI 路徑被使用 Acegi 安全保護,使得沒有 ROLE_USER 角色的用戶無法訪問。 .
          • Security unaware application objects (保護未知的應用對象) ――受保護的對象與 Acegi 之間沒有明顯的耦合或契約,所以它們沒有察覺到安全是由 Acegi 提供的。 *
          • Security taglib usage (安全標簽庫使用) ――所有的 JSP 使用 Acegi 安全標簽庫來封裝安全信息。 *
          • Fully declarative security( 完全聲明式的安全 ) ――每一個安全方面特性都是在 application context 里面使用標準的 Acegi 安全對象來配置的。 *
          • Database-sourced security data (支持數據庫來源的安全數據) ――所有的用戶、角色和 ACL 信息都可以從一個兼容 JDBC 的內存數據庫獲得。
          • Integrated form-based and BASIC authentication (集成基于表單和 BASIC 驗證)―― 任何 BASIC 驗證頭部被檢測以及作為驗證使用。默認使用基于表單的普通交互式驗證。
          • Remember-me services (記住我的服務)―― Acegi 安全的插件式的“ remember-me 策略被演示。在登錄表單里有一個相關的選擇框與之對應。

          聯系人管理的業務功能描述:

          1.1. 每個用戶登錄后,可以看到一個聯系人列表。例如,

          marissa's Contacts

          id

          Name

          Email

          1

          John Smith

          john@somewhere.com

          Del

          Admin Permission

          2

          Michael Citizen

          michael@xyz.com



          3

          Joe Bloggs

          joe@demo.com

          Del


          4

          Karen Sutherland

          karen@sutherland.com

          Del

          Admin Permission

          Add

          說明:用戶沒有權限訪問的聯系人信息,將不會顯示。

          2.2. 用戶可以增加新的聯系人信息。

          3.3. 如果有刪除權限,用戶可以看到在聯系人后面有一個“ Del ”鏈接。用戶可以點擊這個鏈接來刪除某個聯系人信息。

          4.4. 如果有管理權限,用戶可以看到在聯系人后面有一個“ Admin Permission ”鏈接。用戶可以點擊這個鏈接來管理訪問這個聯系人的權限。例如,

          Administer Permissions

          sample.contact.Contact@26807f: Id: 1; Name: John Smith; Email: john@somewhere.com

          -R--- [2] dianne

          Del

          -RW-D [22] peter

          Del

          A---- [1] marissa

          Del

          Add Permission Manage

          說明:每一行記錄包含有 3 列。

          第一列表示權限,例如,“ -RW-D ”表示可讀、可寫、可刪除。

          第二列也表示權限,但它是以類似 unix 權限的數字表達。例如,“ [22] , 表示可讀、可寫、可刪除。

          第三列是用戶名稱。

          每一行記錄后面都有一個“ Del ”鏈接。點擊這個鏈接,可以刪除掉指定用戶對這個聯系人信息的權限。

          5.5. 用戶可以為某個聯系人信息添加權限。例如,

          Add Permission

          Contact:

          sample.contact.Contact@1787005: Id: 1; Name: John Smith; Email: john@somewhere.com


          Recipient:


          Permission:


          說明:權限是動態添加的。例如,上圖中給用戶 scott 增加了讀聯系人 John 的權限。那么 scott 馬上就可以看到聯系人 John 的信息了。

          posted @ 2006-09-01 13:44 Binary 閱讀(637) | 評論 (0)編輯 收藏

          WebWork教程-ServletDispatcher

               摘要: ServletDispatcher 原理 ServletDispatcher 是默認的處理 Web Http 請求的調度器,它是一個 JavaServlet ,是 WebWork 框架的控制器。...  閱讀全文

          posted @ 2006-09-01 13:41 Binary 閱讀(654) | 評論 (0)編輯 收藏

          WebWork教程-validator

          驗證框架
          WebWork 提供了在 Action 執行之前,對輸入數據的驗證功能,它使用了其核心 XWork 的驗證框架。提供了如下功能:
          1、?? 可配置的驗證文件。它的驗證文件是一個獨立的 XML 配置文件,對驗證的添加、修改只需更改配置文件,無需編譯任何的 Class
          2、?? 驗證文件和被驗證的對象完全解藕。驗證對象是普通的 JavaBean 就可以了(可以是 FormBean 、域對象等),它們不需實現任何額外的方法或繼承額外的類。
          3、?? 多種不同的驗證方式。因為它驗證功能是可以繼承的,所以可以用多種不同的方式指定驗證文件,比如:通過父類的 Action 、通過 Action 、通過 Action 的方法、通過 Action 所使用的對象,等等。
          4、?? 強大的表達式驗證。它使用了 OGNL 的表達式語言,提供強大的表達式驗證功能。
          5、?? 同時支持服務器端和客戶端驗證。
          下面我們來看看如何為用戶注冊添加驗證功能:
          1、?? 注冊我們的驗證類型
          WebWork 為不同的驗證要求提供不同的驗證類型。一個驗證類型,一般是有一個類來提供。這個類必須實現接口: com.opensymphony.xwork.validator.Validator ,但我們在寫自己的驗證類型時,無需直接實現 Validator 接口,它有抽象類可供直接使用如 ValidatorSupport FieldValidatorSupport 等。
          驗證類型在使用之前,必須要在 ValidatorFactory com.opensymphony.xwork.validator . ValidatorFactory )中 注冊。可以有二種方法實現驗證類型的注冊。一、寫程序代碼進行注冊,它使用 ValidatorFactory 類的靜態方法: registerValidator(String name, String className) 二、使用配置文件 validators.xml 進行注冊,要求把文件 validators.xml 放到 ClassPath 的跟目錄中( /WEB-INF/classes )。但在實際開發中,一般都使用第二中注冊方法。我們的驗證類型注冊如下:
          <validators>
          ??? <validator name="required" class="com.opensymphony.xwork.validator.validators.RequiredFieldValidator"/>
          ?? ?<validator name="requiredstring" class="com.opensymphony.xwork.validator.validators.RequiredStringValidator"/>
          ??? <validator name="int" class="com.opensymphony.xwork.validator.validators.IntRangeFieldValidator"/>
          ??? <validator name="date" class="com.opensymphony.xwork.validator.validators.DateRangeFieldValidator"/>
          ??? <validator name="expression" class="com.opensymphony.xwork.validator.validators.ExpressionValidator"/>
          ??? <validator name="fieldexpression" class="com.opensymphony.xwork.validator.validators.FieldExpressionValidator"/>
          ??? <validator name="email" class="com.opensymphony.xwork.validator.validators.EmailValidator"/>
          ??? <validator name="url" class="com.opensymphony.xwork.validator.validators.URLValidator"/>
          ??? <validator name="visitor" class="com.opensymphony.xwork.validator.validators.VisitorFieldValidator"/>
          ??? <validator name="conversion" class="com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator"/>
          ??? <validator name="stringlength" class="com.opensymphony.xwork.validator.validators.StringLengthFieldValidator"/>
          </validators>
          注冊驗證類型的配置文件非常簡單。它使用標簽 <validator > 提供名-值對的形式注冊。這樣我們的驗證文件就可以直接引用它的名字。
          2、?? 開啟 Action 的驗證功能
          ? 如果 Action 要使用驗證框架的驗證功能,它必須在配置文件中指定攔截器“ validation ”,它的定義如下:
          <interceptor name="validation" class="com.opensymphony.xwork.validator.ValidationInterceptor"/>
          我們的驗證文件必須以 ActionName-validation.xml 格式命名,它必須被放置到與這個 Action 相同的包中。你也可以為這個 Action 通過別名的方式指定驗證文件,它的命名格式為: ActionName-aliasname-validation.xml 。“ ActionName ”是我們 Action 的類名;“ aliasname ”是我們在配置文件( xwork.xml )中定義這個 Action 所用到的名稱。這樣,同一個 Action 類,在配置文件中的不同定義就可以對應不同的驗證文件。驗證框架也會根據 Action 的繼承結構去查找 Action 的父類驗證文件,如果找到它會去執行這個父類的驗證。
          ?
          3、?? 實現我們的驗證文件: RegisterActionSupport-validation.xml
          <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
          <validators>
          ??? <field name="user.username">
          ??? <field-validator type="requiredstring">
          ??????????? <message>You must enter a value for username.</message>
          ??????? </field-validator>
          ??? </field>
          ??? <field name="user.password">
          ??? <field-validator type="requiredstring">
          ??????????? <message>You must enter a value for password.</message>
          ??????? </field-validator>
          ??????? <field-validator type="fieldexpression">
          ??????????? <param name="expression">user.password == verifyPassword</param>
          ??????????? <message>Passwords don't match.</message>
          ??????? </field-validator>
          ??? </field>
          ??? <field name="user.email">
          ??? <field-validator type="email">
          ??????????? <message>You must enter a valid email.</message>
          ??????? </field-validator>
          ??? </field>
          ??? <field name="user.age">
          ??? <field-validator type="int">
          ??????????? <param name="min">6</param>
          ??????????? <param name="max">100</param>
          ??????????? <message>Age must be between ${min} and ${max}, current value is ${user.age}.</message>
          ?????? ?</field-validator>
          ??? </field>
          </validators>
          說明:
          1 )、 <field > 標簽代表一個字段,它的屬性“ name ”和頁面輸入框的“ name ”屬性必需完全一致,其實它也就是我們的表達式語言。
          2 )、 <field-validator > 標簽定義我們的驗證規則, type 屬性的值就是就是我們前面定義的驗證類型。
          3 )、驗證文件中,字段的數據是通過表達式語言從我們的值堆棧( OgnlValueStack )中取得,一般是 Action Model 對象。例如:我們的字段“ user.age ”,它會通過 Action getUser().getAge() 來取得用戶輸入的年齡,再來根據驗證的類型“ int ”和最大值最小值的參數來判斷輸入的數據是否能通過驗證。
          4 )、不管驗證是否通過,我們的 Action 都會執行,但如果驗證沒有通過,它不會調用 Action execute() 方法。
          ?
          4、?? 顯示 Action 的驗證錯誤信息
          如果用戶輸入的數據驗證沒有通過,我們需重新返回輸入頁面,并給出錯誤信息提示。攔截器棧“ validationWorkflowStack ”為我們實現了這個功能。它首先驗證用戶輸入的數據,如果驗證沒有通過將不執行我們 Action execute() 方法,而是將請求重新返回到輸入頁面。
          我們的 xwork.xml 配置文件如下:
          <action name="registerSupport" class="example.register.RegisterActionSupport">
          ??????????? <result name="success" type="dispatcher">
          ??????????????? <param name="location">/register-result.jsp</param>
          ??????????? </result>
          ??????????? <result name="input" type="dispatcher">
          ??????????????? <param name="location">/registerSupport.jsp</param>
          ??????????? </result>
          ??????????? <interceptor-ref name="validationWorkflowStack"/>
          ??????? </action>
          ?
          通過接口 ValidationAware 我們可以獲得類級別或字段級別的驗證錯誤信息,這個錯誤信息也就是我們驗證文件中 <message> 標簽里的數據。 ActionSupport 類已實現了此接口,這樣在應用中我們的 Action 只要繼承 ActionSupport 類就可以了。 RegisterActionSupport .java 代碼如下:
          package example.register;
          ?
          import com.opensymphony.xwork.ActionSupport;
          ?
          public class RegisterActionSupport extends ActionSupport {
          ?
          ??? private User user= new User();
          ??? private String verifyPassword;
          ???
          ??? public User getUser(){
          ??????? returnthis.user;
          ??? }
          ???
          ??? public String execute(){
          ??????? // 在這里調用用戶注冊的業務邏輯,比如:將注冊信息存儲到數據庫
          ??????? return SUCCESS;
          ??? }
          ?
          ??? public String getVerifyPassword(){
          ??????? returnthis.verifyPassword;
          ??? }
          ???
          ??? publicvoid setVerifyPassword(String verPassword){
          ??????? this.verifyPassword = verPassword;
          ??? }
          }
          我們 WebWork UI 標簽庫直接提供了驗證錯誤信息顯示功能。如果字段級別的驗證沒有通過,它會在輸入框上方顯示驗證文件定義的錯誤提示信息。我們將用戶輸入的頁面更改如下:
          registerSupport.jsp
          <%@ taglib uri="webwork" prefix="ww" %>
          <html>
          <head><title>Register Example</title></head>
          <body>
          <table border=0 width=97%>
          <tr><td align="left">
          ??? <ww:form name="'test'" action="'/example/registerSupport.action'" method="'POST'">
          ??????????? <ww:textfield label="'Username'" name="'user.username'" required="true"/>
          ??????????? <ww:textfield label="'Password'" name="'user.password'" required="true"/>
          ??????????? <ww:textfield label="'VerifyPassword'" name="'verifyPassword'" required="true"/>
          ?????????? ?<ww:textfield label="'Email'" name="'user.email'" required="true"/>
          ??????????? <ww:textfield label="'Age'" name="'user.age'" required="true"/>
          ??????????? <ww:submit value="'Submit'"/>
          ???????? </ww:form>
          </td></tr>
          </table>
          </body>
          </html>
          我們上面的例子使用的是服務器端驗證。 WebWork 也為我們提供了方便的客戶端驗證。它將驗證自動生成 JavaScript 腳本。如果要使用客戶端驗證只需改變相應的驗證類型就可以了(輸入頁面的表單必需使用 <ww:form> 標簽,并設置屬性“ validate="true" ”)。具體的驗證類型可以在 WebWork 的包 com.opensymphony.webwork.validators 中找到。

          posted @ 2006-09-01 13:40 Binary 閱讀(682) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 武城县| 平昌县| 千阳县| 台州市| 孟州市| 科技| 舟曲县| 临猗县| 合山市| 汕尾市| 施秉县| 婺源县| 鄂托克前旗| 精河县| 随州市| 蛟河市| 嵩明县| 辽宁省| 彝良县| 濮阳市| 大英县| 朝阳市| 吉首市| 务川| 乡宁县| 古蔺县| 丹凤县| 赤峰市| 乐清市| 西华县| 冷水江市| 那曲县| 陕西省| 鹿泉市| 广宗县| 黔江区| 无棣县| 赤城县| 楚雄市| 霍邱县| 根河市|