【问题标题】:how to set currentdate as mindate in calendar component of primefaces?如何在primefaces的日历组件中将currentdate设置为mindate?
【发布时间】:2012-06-16 06:12:23
【问题描述】:

我正在使用 primefaces 的日历组件。我想设置mindate=currentdate. 我想限制用户选择当前日期之前的日期。我正在使用 JSF2.0。

【问题讨论】:

  • 是的, 有 mindate 属性。我已经像这样在支持 bean 中获得当前日期,'Calendar curr = Calendar.getInstance(); currentdate = new java.util.Date((curr.getTime()).getTime());'但我不知道如何在 .xhtml 页面的 mindate 属性中写入 currentdate 对象。

标签: jsf-2


【解决方案1】:

只需按照通常的方式将其绑定为 bean 属性即可。

<p:calendar ... mindate="#{bean.currentDate}" />

顺便说一句,您创建当前日期的方式很笨拙。只需调用Date 自己的构造函数就足够了。为此目的使用Calendar 只会带来不必要的开销。

private Date currentDate = new Date();

public Date getCurrentDate() {
    return currentDate;
}

【讨论】:

  • 它适用于 PrimeFaces 3.3。您使用的是哪个 PrimeFaces 版本?
  • PrimeFaces 3.2。什么时候设置mindate属性?这里,实际上没有调用 bean.currentDate 方法。
  • 在 3.2 上也应该可以正常工作。显然您的 EL 语法或 getter 方法签名是错误的。我已经用正确的 getter 方法签名更新了答案。
  • bean is: 'public class adBean { private Date currentDate = new Date(); /** 创建一个新的 adBean 实例 */ public adBean() { } public Date getCurrentDate() { System.out.print("currDate............"+currentDate);返回当前日期; } } '
  • 看起来不错。显然#{adBean} 根本不存在。确保它已正确注册为托管 bean 并在 EL 范围内可用。
【解决方案2】:
<p:calendar mindate="today">

【讨论】:

    【解决方案3】:

    试试这个,

     <p:calendar mindate="calwidget.today()" widgetVar="calwidget" value="#{bean.date}">
    

    【讨论】:

    • 有一个问题:如果你反复打开日历并选择一个新的日期,today() 函数会相应地重新计算。
    【解决方案4】:

    如果您使用的是omnifaces,您可以这样做:

    <p:calendar ... mindate="#{now}" />
    

    【讨论】:

      猜你喜欢
      • 2013-04-29
      • 2014-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-21
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      相关资源
      最近更新 更多