Loading...

          java .net

          開始學(xué)習(xí)struts2(四)

          關(guān)于類型轉(zhuǎn)換還有一種寫法:
          public class ConverterPoint extends StrutsTypeConverter {

          //    @Override
          //    public Object convertValue(Map context, Object value, Class toType) {
          //        if(Point.class == toType){
          //            String[] params = (String[])value;
          //            params = params[0].split(",");
          //            Point point = new Point();
          //            point.setX(Integer.parseInt(params[0]));
          //            point.setY(Integer.parseInt(params[1]));
          //            return point;
          //           
          //        }else if(String.class == toType){
          //            Point point = (Point)value;
          //            return "x=" + point.getX() + ",y=" + point.getY();
          //        }
          //        return super.convertValue(context, value, toType);
          //    }

              @Override
              public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
                  String[] params = arg1;//(String[])value;
                  params = params[0].split(",");
                  Point point = new Point();
                  point.setX(Integer.parseInt(params[0]));
                  point.setY(Integer.parseInt(params[1]));
                  return point;
              }

              @Override
              public String convertToString(Map arg0, Object arg1) {
                  Point point = (Point)arg1;
                  return "x=" + point.getX() + ",y=" + point.getY();
              }

             
          }

          原來是繼承ognl.DefaultTypeConverter類,現(xiàn)在改成繼承org.apache.struts2.util.StrutsTypeConverter類了,看org.apache.struts2.util.StrutsTypeConverter的源碼可知道org.apache.struts2.util.StrutsTypeConverter是個抽象類,并實現(xiàn)了ognl.TypeConverter,org.apache.struts2.util.StrutsTypeConverter中有兩個要實現(xiàn)的方法一個convertFromString,一個convertToString,看方法名字就知道一個從字符串轉(zhuǎn)換成Object,一個是將Object轉(zhuǎn)換成String,這樣好像更加清晰

          另外還有一種全局轉(zhuǎn)換的配置,就是建立一個統(tǒng)一的屬性文件,文件名為xwork-conversion.properties,與struts.xml位于統(tǒng)一目錄下,里面的配置改成了
          com.test.struts2.data.Point=com.test.struts2.action.ConverterPoint
          意思是所有com.test.struts2.data.Point類型的對象都要通過com.test.struts2.action.ConverterPoint來轉(zhuǎn)換

          posted on 2009-01-08 13:30 閱讀(278) 評論(0)  編輯  收藏 所屬分類: 學(xué)習(xí)java

          公告

          希望有一天

          我能用鼠標(biāo)雙擊我的錢包

          然后選中一張100元

          按住“ctrl+c”

          接著不停的“ctrl+v”

          嘻嘻~~~笑醒~~~



          導(dǎo)航

          <2009年1月>
          28293031123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          統(tǒng)計

          常用鏈接

          留言簿(6)

          隨筆分類(102)

          隨筆檔案(398)

          文章分類

          文章檔案(10)

          有趣網(wǎng)絡(luò)

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 新乐市| 桦南县| 锦州市| 玉山县| 镶黄旗| 壤塘县| 呼图壁县| 张家港市| 会宁县| 萨迦县| 马关县| 荔波县| 邳州市| 锦州市| 丹凤县| 清涧县| 南昌县| 安国市| 申扎县| 佛教| 平山县| 甘德县| 四子王旗| 怀来县| 盘山县| 金寨县| 瑞金市| 简阳市| 西林县| 新沂市| 广西| 读书| 大荔县| 彭山县| 安宁市| 怀宁县| 巍山| 泗阳县| 乃东县| 南昌县| 澎湖县|