关于编写类型转换器

本文介绍如何在Struts2框架中实现自定义的日期转换器,以支持从字符串类型转换为Java.util.Date及其子类。通过分析XWork源代码,详细解释了转换器的设计与实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

struts2不支持String到Calendar的转换,参考了一下xwork的源码,想自己写一个。嫌麻烦,还是直接转到java.util.Date吧。把xwork的源代码贴一下,以后写别的转换器时可以参考。 

 

public class XWorkBasicConverter extends DefaultTypeConverter ...{

    
private static String MILLISECOND_FORMAT = ".SSS";
    
final private static SimpleDateFormat RFC3399_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    ......
}

 必须继承自ognl.DefaultTypeConverter这个类;两个常量在做Date转换时会用到,就贴上来了。

完整的从String转换到Date的函数,支持java.sql.Time, java.sql.Timestamp, java.util.Date,简单更改一下即可支持Calendar。

    private Object doConvertToDate(Map context, Object value, Class toType) ...{
        Date result 
= null;

        
if (value instanceof String && value != null && ((String)value).length() > 0...{
            String sa 
= (String) value;
            Locale locale 
= getLocale(context);

            DateFormat df 
= null;
            
if (java.sql.Time.class == toType) ...{
                df 
= DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
            }
 else if (java.sql.Timestamp.class == toType) ...{
                Date check 
= null;
                SimpleDateFormat dtfmt 
= (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT,
                        DateFormat.MEDIUM,
                        locale);
                SimpleDateFormat fullfmt 
= new SimpleDateFormat(dtfmt.toPattern() + MILLISECOND_FORMAT,
                        locale);

                SimpleDateFormat dfmt 
= (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT,
                        locale);

                SimpleDateFormat[] fmts 
= ...{fullfmt, dtfmt, dfmt};
                
for (int i = 0; i < fmts.length; i++...{
                    
try ...{
                        check 
= fmts[i].parse(sa);
                        df 
= fmts[i];
                        
if (check != null...{
                            
break;
                        }

                    }
 catch (ParseException ignore) ...{
                    }

                }

             }
 else if(java.util.Date.class == toType) ...{
                Date check 
= null;
                SimpleDateFormat d1 
= (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale);
                SimpleDateFormat d2 
= (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale);
                SimpleDateFormat d3 
= (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
                SimpleDateFormat[] dfs 
= ...{d1, d2, d3, RFC3399_FORMAT}//added RFC 3339 date format (XW-473)
                for (int i = 0; i < dfs.length; i++...{
                    
try ...{
                        check 
= dfs[i].parse(sa);
                        df 
= dfs[i];
                        
if (check != null...{
                            
break;
                        }

                    }

                    
catch (ParseException ignore) ...{
                    }

                }

            }

            
//final fallback for dates without time
            if (df == null)...{
                df 
= DateFormat.getDateInstance(DateFormat.SHORT, locale);
            }

            
try ...{
                df.setLenient(
false); // let's use strict parsing (XW-341)
                result = df.parse(sa);
                
if (! (Date.class == toType)) ...{
                    
try ...{
                        Constructor constructor 
= toType.getConstructor(new Class[]...{long.class});
                        
return constructor.newInstance(new Object[]...{new Long(result.getTime())});
                    }
 catch (Exception e) ...{
                        
throw new XWorkException("Couldn't create class " + toType + " using default (long) constructor", e);
                    }

                }

            }
 catch (ParseException e) ...{
                
throw new XWorkException("Could not parse date", e);
            }

        }
 else if (Date.class.isAssignableFrom(value.getClass())) ...{
            result 
= (Date) value;
        }

        
return result;
    }

 

完成转换的函数:

public Object convertValue(Map context, Object o, Member member, String s, Object value, Class toType) ...{
        Object result 
= null;

        
if (value == null || toType.isAssignableFrom(value.getClass())) ...{
            
// no need to convert at all, right?
            return value;
        }


        
if (toType == String.class...{
            result 
= doConvertToString(context, value);
        }
 else if (toType == boolean.class...{
            result 
= doConvertToBoolean(value);
       &n
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值