开发者

How to map an IceFaces <ice:selectInputDate> component on a java.util.Calendar field?

Does anybody knows how can component <ice:selectInputDate> be mapped on a java.util.Calendar field, not java.util.Date?

I am using from IceFaces version 1.8.2, the component <ice:selectInputDate>. This component requires to be bound with a java.util.Date proeprty. For example, value="#{bean.myDate}", the myDate field must be of type java.util.Date. But I need my date field to be of type java.util.Calendar.

I have tried to use standard converter or a custom one:

  1. Standard one: <f:convertDateTime pattern="dd/MM/yyyy" /> it formats correct the value in GUI, but when setting it on the property bean.myDate of type Calendar I get following error message:

[5/3/10 12:09:18:398 EEST] 00000021 lifecycle I WARNING: FacesMessage(s) have been enqueued, but may not have been displayed. sourceId=j_id12:j_id189:myDate[severity=(ERROR 2), summary=(/WEB-INF/xhtml............file.xhtml @507,51 value="#{bean.myDate}": Can't set property 'myDate' on class 'bean' to value '5/11/10 3:00 AM'.), detail=(/WEB-INF/xhtml........file.xhtml @507,51 value="#{bean.myDate}": Can't set property 'myDate' on class '...bean...' to value '5/11/10 3:00 AM'.)]

  1. Custom one: <f:co开发者_如何学Gonverter converterId="c2d"/>
  • getAsObject - returns the java.util.Calendar object out of the submitted String.
  • getAsString - receives an Object, and returns the String formatted.

NOTE: this method was hacked so instead of expecting java.util.Calendar, to be complementary with getAsObject method. Instead, the hacked method getAsString, expects an java.util.Date, provided as parameter (by ice:selectInputDate) and returns the String formatted.

But still an error message occurs:

[5/3/10 12:55:34:299 EEST] 0000001f D2DFaceletVie E com.icesoft.faces.facelets.D2DFaceletViewHandler renderResponse Problem in renderResponse: java.util.GregorianCalendar incompatible with java.util.Date java.lang.ClassCastException: java.util.GregorianCalendar incompatible with java.util.Date at com.icesoft.faces.component.selectinputdate.SelectInputDate.getTextToRender(SelectInputDate.java:252)


Wrap the Calendar property with another getter/setter returning/taking a Date.

private Calendar calendar;

public Date getCalendarDate() {
    return (calendar != null) ? calendar.getTime() : null;
}

public void setCalendarDate(Date date) {
    if (calendar == null) {
        calendar = Calendar.getInstance();
        calendar.clear(); // Avoid timezone issues.
    }
    calendar.setTime(date);
}

A JSF converter isn't going to work because this only does Object<-->String conversions, while we need a Object<-->Date conversion here. I don't do IceFaces, but there might be the chance that the particular component accepts a date string in a certain format pattern as well. You would need to find that out and then write the covnerter accordingly to convert Calendar<-->String according this string format pattern. The java.text.SimpleDateFormat is helpful in this.


Either do as BalusC suggests, or simply set value="#{yourBean.yourCalendar.time}.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜