溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫此文的意義!!!煩請(qǐng)您在轉(zhuǎn)載時(shí)注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫此文的意義!!!煩請(qǐng)您在轉(zhuǎn)載時(shí)注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks

          原理

          struts2的自定義類型轉(zhuǎn)換機(jī)制為復(fù)雜類型的輸入輸出處理提供了便捷.struts2已經(jīng)為我們提供了幾乎所有的primitive類型以及常用類型(Date)的類型轉(zhuǎn)換器,我們也可以為我們自定義類添加自定義類型轉(zhuǎn)化器.

          struts2為我們提供了一個(gè)類型轉(zhuǎn)化器的入口: ognl.DefaultTypeConverter,或繼承org.apache.struts2.util.StrutsTypeConverter,由于StrutsTypeConverter提供了更好的封裝,所以建議大家在寫轉(zhuǎn)換器時(shí)通常采用繼承StrutsTypeConverter方式來實(shí)現(xiàn).

          StrutsTypeConverter類實(shí)質(zhì)上是DefaultTypeConverter的擴(kuò)展

           publicabstractclass StrutsTypeConverter extends DefaultTypeConverter

          {

          }

          StrutsTypeConverter中的兩個(gè)核心方法

           publicabstract Object convertFromString(Map context, String[] values, Class toClass);

          publicabstract String convertToString(Map context, Object o);

          convertFromString方法用于從前臺(tái)頁面獲取字符串,將字符串轉(zhuǎn)化為對(duì)象

          convertToString方法用于將對(duì)象以字符串的方式輸出到頁面

          我們?cè)趯?/span>struts2自定義類型轉(zhuǎn)換類的時(shí)候主要就是覆蓋上面兩個(gè)方法

          分類

          struts2自定義類型轉(zhuǎn)換從大的方面來講分兩種:

          u      局部類型轉(zhuǎn)換

          u      全局類型轉(zhuǎn)換

          局部類型轉(zhuǎn)換又分為三種:

          ²       普通實(shí)體bean的自定義類型轉(zhuǎn)換

          ²       基于領(lǐng)域模型的自定義類型轉(zhuǎn)換

          ²       基于模型驅(qū)動(dòng)的自定義類型轉(zhuǎn)換

          無論是全局類型轉(zhuǎn)換還是局部類型轉(zhuǎn)換,轉(zhuǎn)換器與Action之間是用properties文件來關(guān)聯(lián)的,properties文件指明了轉(zhuǎn)換規(guī)則

                 全局類型轉(zhuǎn)換規(guī)則:

                 classpath下新建文件xwork-conversion.properties(固定名稱)

                 其內(nèi)容為:目標(biāo)轉(zhuǎn)換對(duì)象=轉(zhuǎn)換器類(包名+類名)

                 局部類型轉(zhuǎn)換規(guī)則:

                 在對(duì)應(yīng)的Action的同級(jí)目錄下新建Action-conversion.properties(一定要與Action類名對(duì)應(yīng))

                 其內(nèi)容為: 目標(biāo)轉(zhuǎn)換對(duì)象=轉(zhuǎn)換器類(包名+類名)

                 在局部類型轉(zhuǎn)換中又存在一種特殊情況

                 基于領(lǐng)域模型的自定義類型轉(zhuǎn)換

             它不但要在對(duì)應(yīng)的Action的同級(jí)目錄下新建Action-conversion.properties(一定要與Action類名對(duì)應(yīng))文件,還需在引用模型同級(jí)目錄下建properties文件取名規(guī)則為引用名- conversion.properties

          這塊不好用文字描述,舉個(gè)列子:

          需求是這樣的:

          User類中有個(gè)Point對(duì)象的引用,現(xiàn)在要基于Point來做自定義類型轉(zhuǎn)換,這里PointUser之間的這層關(guān)系就叫做領(lǐng)域模型,在操作User時(shí)需要對(duì)Point進(jìn)行自定義類型轉(zhuǎn)換,這時(shí)就必須在User類的同級(jí)目錄下新建User-conversion.properties文件,在文件中指明point對(duì)象需要用什么類來進(jìn)行轉(zhuǎn)換.

          我們約定Point類的對(duì)象名就為point,而對(duì)應(yīng)的轉(zhuǎn)換類為com.dl.convertor.PointConvertor,對(duì)應(yīng)的Action類為PointUserAtion, PointUserAtion中有一個(gè)User類型的屬性名為user

          那么在PointUserAtion的同級(jí)目錄中會(huì)存在一個(gè)名為PointUserAtion-conversion.properties的文件其內(nèi)容為:

          user.point= com.dl.convertor.PointConvertor

          //因?yàn)樵?/span>Action中引用的對(duì)象名為user而現(xiàn)在要處理的是user中的point屬性,所以這里需要使用user.point來指明

          同樣在User類的同級(jí)目錄會(huì)存在一個(gè)名為User-conversion.properties的文件內(nèi)容為

          point=com.dl.convertor.PointConvertor

          //因?yàn)樵撐募会槍?duì)user,所以只需指明User中的point對(duì)象即可不需在添加user否則會(huì)出現(xiàn)預(yù)想不到的結(jié)果

          針對(duì)局部類型轉(zhuǎn)換三種情況的例子

            ²       普通實(shí)體bean類型轉(zhuǎn)換

             實(shí)體bean(Point)

          /**

           *普通的javabean封裝坐標(biāo)

           */

          publicclass Point {

             privateintx;

             privateinty;

          //省略set get方法

          }

          對(duì)應(yīng)的Action(PointAction)

          publicclass PointAction extends ActionSupport{

             private Point point;

          //省略set get方法

             public String execute() throws Exception {

                returnsuper.execute();

             }

          }

          對(duì)應(yīng)的轉(zhuǎn)換類(PointConvertor)

          publicclass PointConvertor extends StrutsTypeConverter{

             /**

              *從表單中的stringPoint對(duì)象

              *我們約定以,來分隔這里為了嚴(yán)謹(jǐn)期間最好要進(jìn)行輸入數(shù)據(jù)的驗(yàn)證

              */

             @Override

             public Object convertFromString(Map context, String[] str, Class c) {

                Point point=null;

                if(str!=null||str.length>0){

                  String[] s=str[0].split(",");

                  point=new Point();

                  int x=Integer.parseInt(s[0]);

                  int y=Integer.parseInt(s[1]);

                  point.setX(x);

                  point.setY(y);

                }

                return point;

             }

             /**

              *從對(duì)象到字符串

              *比如頁面輸出

              */

             @Override

             public String convertToString(Map context, Object o) {

                Point point=(Point)o;

                return"("+point.getX()+","+point.getY()+")";

             }

          }

          需要做的配置:PointAction的同級(jí)目錄下新建PointAction-conversion.properties,文件內(nèi)容為

          #目標(biāo)轉(zhuǎn)換對(duì)象=轉(zhuǎn)換器

          point=com.dl.convertor.PointConvertor



          ²      
          領(lǐng)域模型自定義類型轉(zhuǎn)換

          實(shí)體bean(Point,User)

           publicclass User {

             private String name;

             private Point point;

          //省略set get方法

          }

          publicclass Point {

             privateintx;

             privateinty;

          //省略set get方法

          }

          對(duì)應(yīng)的Action(PointAction)

          public class UserAction extends ActionSupport{

             private User user;

          //省略set get方法

             @Override

             public String execute() throws Exception {

                return super.execute();

             }

          }

          對(duì)應(yīng)的轉(zhuǎn)換類(PointConvertor)

          publicclass PointConvertor extends StrutsTypeConverter{

             /**

              *從表單中的stringPoint對(duì)象

              *我們約定以,來分隔這里為了嚴(yán)謹(jǐn)期間最好要進(jìn)行輸入數(shù)據(jù)的驗(yàn)證

              */

             @Override

             public Object convertFromString(Map context, String[] str, Class c) {

                Point point=null;

                if(str!=null||str.length>0){

                  String[] s=str[0].split(",");

                  point=new Point();

                  int x=Integer.parseInt(s[0]);

                  int y=Integer.parseInt(s[1]);

                  point.setX(x);

                  point.setY(y);

                }

                return point;

             }

             /**

              *從對(duì)象到字符串

              *比如頁面輸出

              */

             @Override

             public String convertToString(Map context, Object o) {

                Point point=(Point)o;

                return"("+point.getX()+","+point.getY()+")";

             }

          }

          需要做的配置:

          UserAction的同級(jí)目錄下新建UserAction-conversion.properties,文件內(nèi)容為

          #目標(biāo)轉(zhuǎn)換對(duì)象=轉(zhuǎn)換器

          user.point=com.dl.convertor.PointConvertor

          User的同級(jí)目錄下新建User-conversion.properties,文件內(nèi)容為

          point=com.dl.convertor.PointConvertor

                  
          ²      
          模型驅(qū)動(dòng)自定義類型轉(zhuǎn)換
          實(shí)體bean(Point)

          /**

           *普通的javabean封裝坐標(biāo)

           */

          publicclass Point {

             privateintx;

             privateinty;

          //省略set get方法

          }

          對(duì)應(yīng)的Action(PointModelDrivenAction)

          /**

           * 基于模型驅(qū)動(dòng)的自定義類型轉(zhuǎn)換

           * @author Administrator

           *

           */

          @SuppressWarnings("serial")

          public class PointModelDrivenAction extends ActionSupport implements ModelDriven<Point>{

             private Point point;

             public Point getPoint() {

                return point;

             }

             public void setPoint(Point point) {

                this.point = point;

             }

             public Point getModel() {

                return point;

             }

             @Override

             public String execute() throws Exception {

                return super.execute();

             }

          }

          :這里切記要生成pointset get方法要不值不能自動(dòng)進(jìn)行封裝

          對(duì)應(yīng)的轉(zhuǎn)換類(PointConvertor)

          publicclass PointConvertor extends StrutsTypeConverter{

             /**

              *從表單中的stringPoint對(duì)象

              *我們約定以,來分隔這里為了嚴(yán)謹(jǐn)期間最好要進(jìn)行輸入數(shù)據(jù)的驗(yàn)證

              */

             @Override

             public Object convertFromString(Map context, String[] str, Class c) {

                Point point=null;

                if(str!=null||str.length>0){

                  String[] s=str[0].split(",");

                  point=new Point();

                  int x=Integer.parseInt(s[0]);

                  int y=Integer.parseInt(s[1]);

                  point.setX(x);

                  point.setY(y);

                }

                return point;

             }

             /**

              *從對(duì)象到字符串

              *比如頁面輸出

              */

             @Override

             public String convertToString(Map context, Object o) {

                Point point=(Point)o;

                return"("+point.getX()+","+point.getY()+")";

             }

          }

          需要做的配置:

          PointModelDrivenAction的同級(jí)目錄下新建PointModelDrivenAction-conversion.properties,文件內(nèi)容為

          #目標(biāo)轉(zhuǎn)換對(duì)象=轉(zhuǎn)換器

          point=com.dl.convertor.PointConvertor

          User的同級(jí)目錄下新建User-conversion.properties,文件內(nèi)容為

          point=com.dl.convertor.PointConvertor




              至此
          Struts2的自定義類型轉(zhuǎn)換你基本已經(jīng)掌握了,還不趕緊動(dòng)手練練.遇到什么問題,請(qǐng)及時(shí)遇我聯(lián)系

          QQ:184675420     Blog:http://www.aygfsteel.com/sxyx2008/

          Email:sxyx2008@gmail.com

          demo下載:http://www.aygfsteel.com/Files/sxyx2008/struts2typeconvertor.zip
                電子文檔下載:點(diǎn)我下載電子版文檔.pdf

          posted on 2010-01-12 14:37 雪山飛鵠 閱讀(10587) 評(píng)論(8)  編輯  收藏 所屬分類: struts2

          Feedback

          # re: Struts2自定義類型轉(zhuǎn)換 2010-01-12 16:19 struts2愛好者
          總結(jié)的很全面,看的出博主是認(rèn)真研究過的  回復(fù)  更多評(píng)論
            

          # re: Struts2自定義類型轉(zhuǎn)換 2010-01-12 16:21 struts2
          圖文結(jié)合,內(nèi)容很豐富,受益了  回復(fù)  更多評(píng)論
            

          # re: Struts2自定義類型轉(zhuǎn)換 2010-01-13 10:08 小猴子
          LZ回避了一個(gè)問題:如果坐標(biāo)分別是在兩個(gè)文本框輸入的情況。這時(shí)如果轉(zhuǎn)換異常,返回時(shí)如何在原頁面顯示?  回復(fù)  更多評(píng)論
            

          # re: Struts2自定義類型轉(zhuǎn)換 2010-01-13 12:45 雪山飛鵠
          @小猴子
          這個(gè)問題好解決嘛,在前臺(tái)頁面文本框輸入處為文本框分別取名為point.x,point.y 然后在后臺(tái)頁面分別獲取point.x,point.y的值然后封裝成point對(duì)象不就解決了.這與用一個(gè)文本框接受坐標(biāo)輸入然后用逗號(hào)來分隔原理是一樣的嘛   回復(fù)  更多評(píng)論
            

          # re: Struts2自定義類型轉(zhuǎn)換 2010-03-07 22:39 當(dāng)當(dāng)
          非常好,一直困擾的問題在你這里解決了,最好再寫一篇struts2模型驅(qū)動(dòng)下的XML驗(yàn)證示例  回復(fù)  更多評(píng)論
            

          # re: Struts2自定義類型轉(zhuǎn)換 2012-05-25 16:54 胖墩兒
          呵呵 對(duì)我很有用!  回復(fù)  更多評(píng)論
            

          # re: Struts2自定義類型轉(zhuǎn)換[未登錄] 2013-04-02 23:05 A
          DFS  回復(fù)  更多評(píng)論
            

          # re: Struts2自定義類型轉(zhuǎn)換[未登錄] 2013-04-02 23:05 A
          FS  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 岳普湖县| 汕头市| 尼木县| 古丈县| 高安市| 新沂市| 醴陵市| 麟游县| 安顺市| 闻喜县| 和林格尔县| 牡丹江市| 乳山市| 常德市| 闽清县| 禹城市| 乾安县| 琼中| 革吉县| 吉水县| 宁武县| 中江县| 闸北区| 乐山市| 清流县| 霍林郭勒市| 军事| 赣州市| 昭苏县| 仙游县| 广元市| 托克逊县| 阆中市| 阿图什市| 北安市| 湘潭市| 衡山县| 贵州省| 叙永县| 永城市| 霍山县|