日期:2014-05-18  浏览次数:20661 次

请教关于DynaValidatorForm的问题~
我用struts1.3.8开发,用的是DynaValidatorForm,有下面一个
<form-bean   name= "roleGroupForm "   type= "org.apache.struts.validator.DynaValidatorForm ">
<form-property   name= "roleGroup "   type= "com.lr.daniel.dakcardsystem.share.db.RoleGroup "/>
<form-property   name= "timeFrom "   type= "java.util.Date "/>
<form-property   name= "timeTo "   type= "java.util.Date "/>
</form-bean>
jsp页面代码:
<%
DynaValidatorForm   form   =   (DynaValidatorForm)request.getAttribute( "roleGroupForm ");
String   _timeFrom,_timeTo;
DateFormat   df   =   new   SimpleDateFormat( "yyyy-MM-dd ");
if   (form   ==   null   ||   form.get( "timeFrom ")   ==   null){
Date   now   =   new   Date();
Date   oneYearAgo   =   new   Date(now.getTime()-365L*24*60*60*1000);
_timeFrom   =   df.format(oneYearAgo);
_timeTo   =   df.format(now);
}else{
_timeFrom   =   df.format((Date)form.get( "timeFrom "));
_timeTo   =   df.format((Date)form.get( "timeTo "));
}
%>
<tr>
<td   class= "labelCss ">
<bean:message   key= "form.search.time.begin "/>
</td>
<td   class= "fieldCss ">
<html:text   styleClass= "date "   property= "timeFrom "   readonly= "true "   onclick= "popCalendar(this) "   value= " <%=_timeFrom%> "   />
</td>
</tr>
<tr>
<td   class= "labelCss ">
<bean:message   key= "form.search.time.end "/>
</td>
<td   class= "fieldCss ">
<html:text   styleClass= "date "   property= "timeTo "   readonly= "true "   onclick= "popCalendar(this) "   value= " <%=_timeTo%> "   />
</td>
</tr>

现在的问题是每次提交都出现下面错误:
org.apache.commons.beanutils.ConversionException:   Cannot   assign   value   of   type   'java.lang.String '   to   property   'timeTo '   of   type   'java.util.Date '
org.apache.struts.action.DynaActionForm.set(DynaActionForm.java:452)
org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:1733)
org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1648)
org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:1677)
org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1022)
org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:811)

不知道哪位打下遇到过没?~~
我以前用struts1.2.X开发就没出现过这种错误~
是不是struts1.3.8的问题造成的?


------解决方案--------------------
..
这是类型转换的问题.你要struts自动把string类型转换成Date类型 ,那么你必须要让struts知道你要将string转换成Date,你就要实现继承Converter这个接口的类,并进行注册
------解决方案--------------------
value转成String试试看
------解决方案--------------------
以前也用过DynaValidatorForm,遇到日期类型的都是用String来处理的,你在formbean里面将日期的类型换成String试试,然后在程序里面解析一下这个字符串得到Date
String timeFrom = (String)form.get( "timeFrom "); //从页面传过来的日期串应该是 "yyyy-MM-dd &qu