Struts 1 ActionForm 中的处理日期
我在处理 java.util.Date
的输入请求参数(当然是 String 类型)时遇到问题。我认为添加到我的 bean 中的以下代码可能会解决这个问题,但我错了:
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public void setDate(String dateString) {
try {
date = DateFormat.getDateInstance().parse(dateString);
} catch (ParseException e) {
date = new Date();
}
}
它在提交表单后抛出异常:
javax.servlet.ServletException:BeanUtils.populate org.apache.struts.util.RequestUtils.populate(RequestUtils.java:469) org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:818) java.lang.IllegalArgumentException:无法调用 com.epam.testapp.model.News.setDate - 参数类型不匹配 org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1778) org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:1759) org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1648) org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:1677)
由于返回 getter 和接受 setter 参数类型不匹配,这段代码无法工作,这是 struts 1 形式的基础吗?我该如何解决这个问题?我根本不想创建像 setStringDate(String stringDate(){...}
这样的方法并在每个页面上思考我应该调用哪个方法:(
I have problem on processing input request parameter (of course it's type String) to java.util.Date
. I thought that following code added to my bean might solve this problem, but I was wrong:
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public void setDate(String dateString) {
try {
date = DateFormat.getDateInstance().parse(dateString);
} catch (ParseException e) {
date = new Date();
}
}
It throws an exception after submiting form:
javax.servlet.ServletException: BeanUtils.populate org.apache.struts.util.RequestUtils.populate(RequestUtils.java:469) org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:818) java.lang.IllegalArgumentException: Cannot invoke com.epam.testapp.model.News.setDate - argument type mismatch org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1778) org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:1759) org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1648) org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:1677)
Is this fundamental of struts 1 form that this code won't work because of mismatch of returning getter and accepting setter parameter types? How can I solve this problem ? I don't want at all to make method named like setStringDate(String stringDate(){...}
and think on every page which method should I call :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
日期对象不能是 struts 中的属性,因为日期格式可能会有所不同(取决于规范)。有些可能有 dd-MM-yyyy 、 dd-MMMM-yy 等。
我建议有一个属性:
在您的操作中,将日期字符串转换为
日期
对象。Date object cannot be a property in struts as date format can vary (depending on specification). Some may have
dd-MM-yyyy
,dd-MMMM-yy
, etc.I would suggest having a property:
And in your action, convert the date string into
Date
object.据我所知,我认为重载方法在表单 bean 中不能很好地工作。尝试以不同的方式命名这两种方法,我认为您会有更好的运气。
As per my knowledge I think , overloaded methods don't work very well in form beans .Try naming the two methods differently, and I think you'll have better luck.