让你的struts能处理日期类型

来源:互联网 发布:苹果电脑的录音软件 编辑:程序博客网 时间:2024/05/16 08:44
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

  在http://www.javaresearch.org/forum/thread.jsp?column=20&thread=9712中有不少朋友问如何让struts处理日期类型,我现在在这儿综合回答一下,希望能提供struts开发人员效率。

  struts使用日期包括将string自动转化为日期fill到form中,以及将form中的日期按照指定格式显示在html的textfield中。首先讲第一种情况的解决方法:

  创建如下类:

  importjava.util.*;

  importorg.apache.commons.beanutils.*;

  importjava.text.SimpleDateFormat;

  publicclassDateConvertimplementsConverter

  {

  staticSimpleDateFormatdf=newSimpleDateFormat("yyyy/MM/dd");

  publicDateConvert()

  {

  }

  publicObjectconvert(Classtype,Objectvalue)

  {

  if(value==null)returnnull;

  if(((String)value).trim().length()==0)returnnull;

  if(valueinstanceofString)

  {

  try

  {

  returndf.parse((String)value);

  }

  catch(Exceptionex)

  {

  thrownewConversionException("输入的日期类型不合乎yyyy/MM/dd"

  +value.getClass());

  }

  }

  else

  {

  thrownewConversionException("输入的不是字符类型"+value.getClass());

  }

  }

  }

  然后在你的系统某出使用如下(如web的init方法)

  ConvertUtils.register(newDateConvert(),java.util.Date.class);

  参数用于表示DateConvert类负责处理java.util.Date类型的转化

  第二种情况是如何显示form中日期类型到html:text中,我用的办法是修改struts的代码,重新生成一个新的struts.jar

  org.apache.struts.taglib.html.BaseFieldTag的doStartTag的方法

  找到if(value!=null){results.append(ResponseUtils.filter(value))代码行下面的内容,需要修改此处代码,以便输出日期类型

  如下:

  if(value!=null){

  results.append(ResponseUtils.filter(value));

  }elseif(redisplay||!"password".equals(type)){

  Objectvalue=RequestUtils.lookup(pageContext,name,property,null);

  //System.out.println("lijz"+value);

  if(valueinstanceofjava.util.Date)

  {

  //System.out.println("date="+value);

  if(value==null)

  value="";

  else

  {

  java.util.Dated=(java.util.Date)value;

  try

  {

  results.append(ResponseUtils.filter(df.format(d)));

  }

  catch(Exceptionex)

  {

  System.out.println("formerror:"+ex.getMessage());

  }

  }

  }

  else

  {

  if(value==null)

  value="";

  results.append(ResponseUtils.filter(value.toString()));

  }

  }

  results.append("/"");

  results.append(prepareEventHandlers());

  results.append(prepareStyles());

  results.append(getElementClose());

  //Printthisfieldtoouroutputwriter

  ResponseUtils.write(pageContext,results.toString());

  //Continueprocessingthispage

  return(EVAL_BODY_TAG);

  重新编译strutsstruts.jar.放到你需要的项目中

  原理不用多说,看看struts源代码就明白了:)

  希望能提高大家的开发效率

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>