城市獵人

          在一網情深的日子里,誰能說得清是苦是甜,只知道確定了就義無反顧
          posts - 1, comments - 7, trackbacks - 0, articles - 89

          BeanUtils日期轉換

          Posted on 2009-03-24 12:26 sailor 閱讀(1030) 評論(0)  編輯  收藏 所屬分類: 心得體會java
                  經過一天測試,終于實現了BeanUtils從String類型到Timestamp成功轉換。底層是如何實現的,還沒徹底了解,先將解決方案記錄下來,以后再去看底層如何實現。
                     1、寫一個日期轉換器,這個轉換器實現了Converter接口
           1public class DateConvert implements Converter {
           2        private Logger log = Logger.getLogger(DateConvert.class);
           3        static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
           4        private String datePattern = "yyyy-MM-dd";
           5
           6        public DateConvert() 
           7            
           8        }
           
           9        
          10        public DateConvert(String datePattern)
          11        {
          12            this.datePattern = datePattern;
          13        }

          14    
          15        /**
          16         *  @type  需要被轉換的類型
          17         *  @value 被轉換的值
          18         */

          19        public Object convert(Class type, Object value) 
          20            
          21            if(value==null)
          22                 return null
          23            
          24            if(((String)value).trim().length()==0)
          25                return null
          26            
          27            if(value instanceof String) 
          28                try 
          29                    //解析接收到字符串 
          30                    return TimestampUtil.parseTimestamp((String)value,datePattern);   //自己寫的封裝,得到一個Timestamp實例
          31                    
          32                }
           catch (Exception ex) {
          33                    //發生解析異常
          34                    throw new ConversionException("輸入的日期類型不合乎" + datePattern + "格式" + value.getClass()); 
          35                }
           
          36            }
           else 
          37                //其他異常 
          38                throw new ConversionException("輸入的不是字符類型" + value.getClass()); 
          39            }
           
          40        }

          41}

                  2、自己重寫了一個BeanUtil,用于實現拷貝request傳遞來的參數到Model里面,里面注冊了一個日期處理監聽,這個組件還有待改進
           1public class BeanUtil {
           2
           3    public void copyBean(){};
           4    
           5    /**
           6     * 將request里的參數封裝到一個Bean。如果bean里面有日期型,只是java.sql.Date,java.sql.TimeStamp類型
           7     * @param request
           8     * @param bean
           9     * @throws IllegalAccessException
          10     * @throws InvocationTargetException
          11     */

          12    public static void copyRequestPrameterToModel(HttpServletRequest request, Object bean) throws IllegalAccessException, InvocationTargetException{
          13        Map map = getRequestPrameters(request);
          14        
          15        if(map == null)
          16            return ;    
          17        
          18        ConvertUtils.register(new DateConvert(), java.sql.Timestamp.class); //注冊監聽
          19        BeanUtils.populate(bean, map);
          20    }
          ;
          21    
          22    /**
          23     * 從request中取出key和value封裝到Map里
          24     * @param request
          25     * @return
          26     */

          27    private static Map<String,Object> getRequestPrameters(HttpServletRequest request)
          28    {
          29        Map<String,Object> map = new HashMap<String,Object>();
          30        
          31        Enumeration enuma = request.getParameterNames();
          32        while(enuma.hasMoreElements())
          33        {
          34            String key = (String)enuma.nextElement();
          35            
          36            String value = (String)request.getParameter(key);    //獲取value
          37            
          38            //如果值為空的話,就不插入map里
          39            if(null == value || "".equals(value))
          40            {
          41                continue;
          42            }

          43            
          44            map.put(key, request.getParameter(key));
          45        }

          46        
          47        return map;
          48    }

          49}

           

             


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 石渠县| 扬中市| 哈巴河县| 工布江达县| 铜川市| 丰城市| 连南| 防城港市| 芜湖县| 元江| 新化县| 丰都县| 镇赉县| 东台市| 永仁县| 轮台县| 大名县| 出国| 和政县| 久治县| 江北区| 广宁县| 宜昌市| 浙江省| 云龙县| 宁化县| 色达县| 于都县| 清原| 湛江市| 保德县| 浦县| 辉南县| 三门峡市| 临城县| 曲沃县| 句容市| 怀远县| 武城县| 徐闻县| 沂源县|