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

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

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

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

    雪山飛鵠

    溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫此文的意義!!!煩請您在轉(zhuǎn)載時注明出處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為我們提供了一個類型轉(zhuǎn)化器的入口: ognl.DefaultTypeConverter,或繼承org.apache.struts2.util.StrutsTypeConverter,由于StrutsTypeConverter提供了更好的封裝,所以建議大家在寫轉(zhuǎn)換器時通常采用繼承StrutsTypeConverter方式來實現(xiàn).

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

     publicabstractclass StrutsTypeConverter extends DefaultTypeConverter

    {

    }

    StrutsTypeConverter中的兩個核心方法

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

    publicabstract String convertToString(Map context, Object o);

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

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

    我們在寫struts2自定義類型轉(zhuǎn)換類的時候主要就是覆蓋上面兩個方法

    分類

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

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

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

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

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

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

    ²       基于模型驅(qū)動的自定義類型轉(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)換對象=轉(zhuǎn)換器類(包名+類名)

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

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

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

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

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

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

    這塊不好用文字描述,舉個列子:

    需求是這樣的:

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

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

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

    user.point= com.dl.convertor.PointConvertor

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

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

    point=com.dl.convertor.PointConvertor

    //因為該文件只針對user,所以只需指明User中的point對象即可不需在添加user否則會出現(xiàn)預(yù)想不到的結(jié)果

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

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

       實體bean(Point)

    /**

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

     */

    publicclass Point {

       privateintx;

       privateinty;

    //省略set get方法

    }

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

    publicclass PointAction extends ActionSupport{

       private Point point;

    //省略set get方法

       public String execute() throws Exception {

          returnsuper.execute();

       }

    }

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

    publicclass PointConvertor extends StrutsTypeConverter{

       /**

        *從表單中的stringPoint對象

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

        */

       @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;

       }

       /**

        *從對象到字符串

        *比如頁面輸出

        */

       @Override

       public String convertToString(Map context, Object o) {

          Point point=(Point)o;

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

       }

    }

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

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

    point=com.dl.convertor.PointConvertor



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

    實體bean(Point,User)

     publicclass User {

       private String name;

       private Point point;

    //省略set get方法

    }

    publicclass Point {

       privateintx;

       privateinty;

    //省略set get方法

    }

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

    public class UserAction extends ActionSupport{

       private User user;

    //省略set get方法

       @Override

       public String execute() throws Exception {

          return super.execute();

       }

    }

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

    publicclass PointConvertor extends StrutsTypeConverter{

       /**

        *從表單中的stringPoint對象

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

        */

       @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;

       }

       /**

        *從對象到字符串

        *比如頁面輸出

        */

       @Override

       public String convertToString(Map context, Object o) {

          Point point=(Point)o;

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

       }

    }

    需要做的配置:

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

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

    user.point=com.dl.convertor.PointConvertor

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

    point=com.dl.convertor.PointConvertor

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

    /**

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

     */

    publicclass Point {

       privateintx;

       privateinty;

    //省略set get方法

    }

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

    /**

     * 基于模型驅(qū)動的自定義類型轉(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方法要不值不能自動進(jìn)行封裝

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

    publicclass PointConvertor extends StrutsTypeConverter{

       /**

        *從表單中的stringPoint對象

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

        */

       @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;

       }

       /**

        *從對象到字符串

        *比如頁面輸出

        */

       @Override

       public String convertToString(Map context, Object o) {

          Point point=(Point)o;

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

       }

    }

    需要做的配置:

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

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

    point=com.dl.convertor.PointConvertor

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

    point=com.dl.convertor.PointConvertor




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

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

    Email:sxyx2008@gmail.com

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

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

    Feedback

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

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

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

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

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

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

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

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

    主站蜘蛛池模板: 亚洲欧美成人一区二区三区| 永久中文字幕免费视频网站| 国色精品va在线观看免费视频| 一级人做人a爰免费视频| 亚洲a∨国产av综合av下载 | 无码高潮少妇毛多水多水免费| 久久这里只精品热免费99| 亚洲精品国产日韩| 你懂的在线免费观看| 亚洲最大在线观看| 亚洲av无码一区二区三区天堂古代 | 亚洲精品无码专区在线| 亚洲欧美精品午睡沙发| WWW国产亚洲精品久久麻豆| 免费在线观看一区| 中文字幕无码免费久久9一区9| 巨胸狂喷奶水视频www网站免费| 国产成人1024精品免费| h视频在线免费观看| 成全视频免费观看在线看| 一级毛片免费视频| 91视频国产免费| 日韩在线看片免费人成视频播放| 大胆亚洲人体视频| 黑人大战亚洲人精品一区 | 国产乱子伦精品免费无码专区| 亚洲 无码 在线 专区| 国产成人亚洲精品狼色在线| 久久精品国产精品亚洲艾| 亚洲欧洲在线播放| 亚洲爆乳少妇无码激情| 国产成人无码精品久久久久免费 | 亚洲另类无码专区首页| 日韩免费无砖专区2020狼| 国产精品白浆在线观看免费| 亚洲综合精品香蕉久久网| 亚洲国产精品自在在线观看| 亚洲免费在线视频观看| 亚洲AV无码专区在线厂| 成人影片一区免费观看| 五月婷婷综合免费|