如何映射IceFaces< ice:selectInputDate> java.util.Calendar字段上的组件? [英] How to map IceFaces <ice:selectInputDate> component on a java.util.Calendar field?

查看:120
本文介绍了如何映射IceFaces< ice:selectInputDate> java.util.Calendar字段上的组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何将< ice:selectInputDate> 映射到 java.util.Calendar 字段,而不是 java.util.Date

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

我使用 IceFaces 版本1.8.2,组件< ice:selectInputDate> 。此组件需要绑定 java.util.Date proeprty。例如, value =#{bean.myDate} myDate 字段必须是<$ c类型$ C> java.util.Date 。但我需要我的日期字段为 java.util.Calendar

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. 标准一:< f:convertDateTime pattern = dd / MM / yyyy/> 它格式化了GUI中的值,但在属性上设置 bean.myDate 的类型日历我收到以下错误消息:

  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
生命周期I警告:FacesMessage(s)
已入队,但可能没有显示

sourceId = j_id12:j_id189:myDate [severity =(ERROR 2),
summary =(/ WEB-INF / xhtml ............ file.xhtml @ 507, 51 value =#{bean.myDate}:不能
将类'bean'
上的属性'myDate'设置为值'5/11/10 3:00 AM'。),
detail =(/ WEB-INF / xhtml ........ file.xhtml
@ 507,51 value =#{bean.myDate}:不能设置

上的属性'myDate''... bean ...'值'5/11/10 3:00
AM'。)]

[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'.)]

自定义一:< f:converter converterId =c2d/>


  • getAsObject - 从提交的字符串中返回 java.util.Calendar 对象

  • getAsString - 收到对象,并返回 String 格式化。

  • getAsObject - returns the java.util.Calendar object out of the submitted String.
  • getAsString - receives an Object, and returns the String formatted.

注意:这种方法被黑客攻击期待 java.util.Calendar ,与 getAsObject 方法互补。相反,被黑客入侵的方法 getAsString ,需要一个 java.util.Date ,作为参数提供(通过 ice:selectInputDate )并返回 String 格式化。

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
renderResponse中的问题:
java.util.GregorianCalendar
与java.util.Date不兼容
java。 lang.ClassCastException:java.util.GregorianCalendar
与com.icesoft.faces.component.selectinputdate.SelectInputDate.getTextToRender(SelectInputDate.java:252)中的java.util.Date
不兼容

[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)

任何提示都非常有用!
谢谢,
Mihaela

Any hint is very useful! Thanks, Mihaela

推荐答案

包装日历另一个getter / setter的属性返回/获取日期

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转换器无法正常工作,因为这只能 Object < - > 字符串转换,而我们需要对象< - > 日期此处转换。我不做IceFaces,但特定组件也可能接受某种格式模式的日期字符串。你需要找到它然后相应地编写covnerter来转换 Calendar < - > String 字符串格式模式。 java.text.SimpleDateFormat 对此有帮助。

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.

这篇关于如何映射IceFaces&lt; ice:selectInputDate&gt; java.util.Calendar字段上的组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆