jackyrong

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

  在struts2中,比较反感struts2本身的datatime picker,因为听说问题很多,于是可以网上找些开源的日期的js效果,使用时,这样
 <s:textfield label="出版日期" name="book.bookDate" onfocus="new WdatePicker(this)"/>

在xxx.hbm.xml中设置

    <property name="bookDate" type="java.util.Date">
            <column name="book_date"  length="30"/>

mysql中是date类型

还要写个转换器,放在src目录下,命名为xwork-conversion.properties

  java.util.Date=xxxx.DateConverter

DateConver类 
 

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import ognl.DefaultTypeConverter;
import ognl.ParseException;

 public class DateConverter extends DefaultTypeConverter { 
         private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
      
         public Object convertValue(Map ognlContext, Object value, Class toType) {          
             Object result = null; 
             if (toType == Date.class) { 
                 result = doConvertToDate(value); 
             } else if (toType == String.class) { 
                result = doConvertToString(value); 
            } 
            return result; 
        } 
     
        private Date doConvertToDate(Object value) { 
            Date result = null; 
     
            if (value instanceof String) { 
                try { 
                    result = sdf.parse((String) value); 
                } catch (Exception e) { 
                    //e.printStackTrace();
                 System.out.println("dateconverter is failed!");
                } 
            } else if (value instanceof Object[]) { 
                // let's try to convert the first element only 
                Object[] array = (Object[]) value; 
                if ((array != null) && (array.length >= 1)) { 
                   value = array[0]; 
                    result = doConvertToDate(value); 
                } 
            } else if (Date.class.isAssignableFrom(value.getClass())) { 
                result = (Date) value; 
            } 
            return result; 
        } 
     
        private String doConvertToString(Object value) { 
            String result = null; 
            if (value instanceof Date) { 
                result = sdf.format(value); 
            } 
            return result; 
        } 
    }  

posted on 2008-10-07 21:17  jackyrong的世界  阅读(1556)  评论(0编辑  收藏  举报