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

邮差的信 提交于 2019-12-31 02:50:07

问题


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.

My trials: 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'.)]

  2. Custom one: <f:converter 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)

Any hint is very useful! Thanks, Mihaela


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/2766287/how-to-map-icefaces-iceselectinputdate-component-on-a-java-util-calendar-fiel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!