Struts 1 ActionForm 中的处理日期

发布于 2024-12-15 11:45:20 字数 1307 浏览 2 评论 0原文

我在处理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淤浪 2024-12-22 11:45:20

日期对象不能是 struts 中的属性,因为日期格式可能会有所不同(取决于规范)。有些可能有 dd-MM-yyyy 、 dd-MMMM-yy 等。

我建议有一个属性:

private String date;

public String getDate() { return date; }

public void setDate(String date) { this.date = date; }

在您的操作中,将日期字符串转换为 日期对象。

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:

private String date;

public String getDate() { return date; }

public void setDate(String date) { this.date = date; }

And in your action, convert the date string into Date object.

冰葑 2024-12-22 11:45:20

据我所知,我认为重载方法在表单 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文