【问题标题】:<rich:calendar> doesn't accept java.util.Calendar<rich:calendar> 不接受 java.util.Calendar
【发布时间】:2011-06-17 15:18:24
【问题描述】:

我正在尝试使用rich:calendar,当日期属性selectedDate 和currentDate 是Date 类型时它可以工作。当我使用Calendar 时,它不起作用。

当我提交表单时,它会报告:

SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/PORTAL] threw exception [javax.el.ELException: /test.xhtml @58,36 value="#{bean.calendar.selectedDate}": Cannot convert 24.6.11 0:00 of type class java.util.Date to class java.util.Calendar] with root cause
javax.el.ELException: /test.xhtml @58,36 value="#{bean.calendar.selectedDate}": Cannot convert 24.6.11 0:00 of type class java.util.Date to class java.util.Calendar

我的 XHTML:

<rich:calendar 
  mode="ajax" id="calendar"
  value="#{bean.calendar.selectedDate}"  
  locale="#{calendarBean.locale}"  
  datePattern="#{calendarBean.pattern}"                        
  required="true">                   
</rich:calendar>

还有日历豆:

@ManagedBean
public class CalendarBean
{    
  private Calendar currentDate;  
  private Calendar selectedDate;

  public Calendar getCurrentDate() {  
    return currentDate;  
  }  
  public void setCurrentDate(Calendar currentDate) {  
    this.currentDate = currentDate;  
  } 
  public Calendar getSelectedDate() { 
    return selectedDate;  
  }  
  public void setSelectedDate(Calendar selectedDate) {  
    this.selectedDate = selectedDate;  
  }

我做错了什么?

【问题讨论】:

  • @mre:这毫无意义。在使用公历的语言环境中,Calendar 是GregorianCalendar已经。再看,Calendar 是一个接口。您可以通过System.out.println(Calendar.getInstance().getClass()) 找到您的Calendar 类的具体实现。所有其他实现的行为不应有所不同。
  • @BalusC,嗯..我想我明白你在说什么。我建议GregorianCalendar,因为它是一个类,而不是一个接口。如果不正确,我会删除我的建议。 :)
  • @mre:多研究一下接口和多态性。玩一会儿 JDBC 和不同的 DB,直到你最终意识到接口的力量。

标签: java jsf richfaces


【解决方案1】:

Calendar 类有一个time 属性,它代表一个java.util.Date,并带有一个getter 和setter。好好利用吧。

value="#{bean.calendar.selectedDate.time}"  

【讨论】:

  • 谢谢,但现在:javax.el.PropertyNotFoundException: /test.xhtml @58,36 value="#{bean.calendar.selectedDate.time}": 目标无法到达,'selectedDate' 返回 null ......?
  • 你需要确保它不为空。在CalendarBean 的构造函数中实例化它。例如。 this.selectedDate = Calendar.getInstance(); 等等。
  • 谢谢你,BalusC,等我学完我给你发个标题:)
  • 你的回答救了我@BalusC
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-11
  • 2010-11-14
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-25
  • 1970-01-01
相关资源
最近更新 更多