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

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

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

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

    雪山飛鵠

    溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫此文的意義!!!煩請(qǐng)您在轉(zhuǎn)載時(shí)注明出處http://www.tkk7.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.tkk7.com/sxyx2008/

    Email:sxyx2008@gmail.com

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

    posted on 2010-01-12 14:37 雪山飛鵠 閱讀(10584) 評(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)論
      

    主站蜘蛛池模板: 亚洲免费在线视频| 永久免费毛片在线播放| 亚色九九九全国免费视频| 日本中文一区二区三区亚洲| 亚洲av无码成人黄网站在线观看| 亚洲首页国产精品丝袜| 亚欧乱色国产精品免费视频| 很黄很黄的网站免费的| 国产成人精品亚洲精品| 亚洲色大成WWW亚洲女子| 青柠影视在线观看免费高清| 免费鲁丝片一级观看| 亚洲AV无码成人网站久久精品大| 337P日本欧洲亚洲大胆艺术图| 99久久人妻精品免费一区| 免费午夜爽爽爽WWW视频十八禁| 一区二区三区亚洲| 牛牛在线精品观看免费正| 五月婷婷综合免费| 国产亚洲3p无码一区二区| 亚洲AV日韩AV永久无码色欲| 91制片厂制作传媒免费版樱花| 亚洲国产精品人人做人人爽| 亚洲一区在线免费观看| 99久久精品毛片免费播放| 日本免费一区二区三区最新| 91亚洲导航深夜福利| 国产精品永久免费| 国产男女猛烈无遮挡免费视频| 亚洲天天在线日亚洲洲精| 一级午夜免费视频| 永久黄网站色视频免费| 337p日本欧洲亚洲大胆艺术| 久久久久久国产a免费观看不卡| 日韩人妻无码免费视频一区二区三区| 亚洲伊人久久大香线蕉苏妲己| 精品国产免费一区二区三区| 国产在线观看www鲁啊鲁免费| 亚洲乱码一区av春药高潮| 日韩电影免费在线观看| 不卡精品国产_亚洲人成在线|