【问题标题】:Primefaces calendar does not set the datePrimefaces 日历未设置日期
【发布时间】:2017-12-24 10:37:20
【问题描述】:

我对 Primefaces 的工作逻辑还有一个误解。

目前<p:calendar> 组件在我按下提交按钮时无法设置选定日期。
我看到它成功进入 actionListener 方法,但那里的日期值为空。

首先,我尝试使用标准 PF example 创建我的日历。它看起来太简单了,我认为当用户选择日期或提交表单时,组件应该调用 value setter。但它既没有在第一种情况下也没有在第二种情况下这样做。 嗯,我打开谷歌,发现了几个帖子:

Primefaces Calendar Setting Date Value in Backing Bean
p:calendar value not set in backing bean

我确保我的日历位于 <h:form></h:form> 标记之间。我还尝试添加process="@this"process=":beginDateForm :endDateForm @this",其中beginDateForm 和endDateForm 是包含<p:calendar> 组件的表单。
我还找到了post 并尝试创建 SelectEvent 侦听器方法:

private void changeDate(SelectEvent event) {
    beginDate = (Date) event.getObject();
}

但没有成功。

我还尝试使用 valueChangeListener 更改日期:

<h:form id="beginDateForm">
    <p:calendar id="passBeginDate" valueChangeListener="#{territoryFormBean.changeDate}" mode="popup" readonly="true" pattern="dd.MM.yyyy" showOn="button" value="#{territoryFormBean.beginDate}" />
</h:form>

当然,我将事件更改为 ValueChangeEvent。

之后,我将 &lt;p:calendar&gt;&lt;p:commandButton&gt; 组件移动到同一个 &lt;h:form&gt; 并尝试了两个不同的过程值 process="passTerrForm:passBeginDate passTerrForm:passEndDate @this"process="@form @this"process="@form" 在最后一种情况下,按钮甚至不会触发侦听器方法。

我目前的组件是:

<p:commandButton value="Search" id="searchPassButton" actionListener="#{territoryFormBean.search}" update=":passTerrForm:territoryTable" process="passTerrForm:passBeginDate passTerrForm:passEndDate @this" partialSubmit="true" />

<p:column>
    <p:calendar id="passBeginDate" mode="popup" readonly="true" pattern="dd.MM.yyyy" showOn="button" value="#{territoryFormBean.beginDate}" />
</p:column>
<p:column>
    <p:calendar id="passEndDate" mode="popup" readonly="true" pattern="dd.MM.yyyy" showOn="button" value="#{territoryFormBean.endDate}" />
</p:column>

伙计们,您能提出其他建议吗?或者您可能会看到我的代码中有什么问题。 我不明白为什么组件不调用 setter。

【问题讨论】:

  • 代码报错?
  • @PolRodríguez 没有。它不是。我检查了 NetBeans 输出,发现只有“创建组件”之类的 Fine 和 Finest 消息
  • 顺便说一下,我还检查了位于同一页面中的输入字段是否正确提交。我可以在调试模式下看到 java 字段具有正确的值。
  • territoryFormBean的范围是什么?
  • @XtremeBiker,最初 bean 是 ViewScoped。我尝试将其更改为 SessionScoped,但没有成功

标签: jsf primefaces calendar


【解决方案1】:

好吧,伙计们,我发现了我的错误。愚蠢的错误。另一个重新阅读 PF 文档表明,使用 readonly 参数不适合我的目标。我想阻止手动日期直接输入到&lt;p:calendar&gt; 文本字段中。但根据文档: readonly -

指示此输入元素将阻止由 用户

但我需要readonlyInput参数

将弹出日历的输入文本设为只读。

所以第二个参数阻止输入,而第一个参数完全阻止更改。
感谢您的帮助。

【讨论】:

    【解决方案2】:

    尝试与我的代码进行类比,我认为将按钮放入与日历相同的形式,并省略“流程”和“部分提交”应该可以:

        <h:form id="beginDateForm">
         <p:calendar id="passBeginDate" valueChangeListener="#{territoryFormBean.changeDate}" mode="popup" readonly="true" pattern="dd.MM.yyyy" showOn="button" value="#{territoryFormBean.beginDate}" />
         <p:commandButton value="Search" id="searchPassButton" actionListener="#{territoryFormBean.search}" update=":passTerrForm:territoryTable" />
        </h:form>
    

    希望对你有帮助!

    【讨论】:

    • 嗨!感谢您的回复。我试图这样做,但不幸的是它没有帮助。所有 inputText 字段和选择菜单都传递值,但日历不是 :(
    • 你也应该在上面的代码中省略 valueChangeListener,我在回复中跳过了这个细节。
    • 好吧,现在我删除了 valueChangeListener 并返回到初始教程配置。但我无法删除“进程”参数。否则表单甚至不会将 inputText 字段值传递到托管 bean。
    【解决方案3】:

    JSF 代码:

    <p:calendar id="from" value="#{pageBean.event.startDate}"   pattern="MM/dd/yyyy hh:mm:ss a" timeZone="GMT-4"/>
    
    <p:commandButton id="addButton" value="Save" actionListener="#{pageBean.addEvent}" update=":@form"/>
    

    您可以在需要将这些日历事件添加到 eventModel 并保存的 Bean 中编写 addEvent 方法。这可以帮助您设置日期,并且您可以在需要时检索它。

    Java 代码:

    private ScheduleModel eventModel;
    private ScheduleEvent event = new DefaultScheduleEvent();
    public String addEvent(ActionEvent actionEvent) {    
    
       if(event.getId() == null){
            eventModel.addEvent(event);
       }
    }
    

    希望这会有所帮助!

    【讨论】:

    • 我还是看不懂事件模型的概念,我去研究一下。但它确实看起来很奇怪。正如我在 PF 教程中看到的,可以简单地通过 java 后端中具有 Date 类型的 value 参数传递日期。
    • 您是否为日历活动保存了工作?
    • 我现在不需要手动处理日历事件。对于我的目标,只需选择日期并通过提交按钮将其传递给托管 bean 就足够了。正如我已经写过的,我发现了错误,可以使用默认方式来设置日期。
    猜你喜欢
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 2011-06-06
    相关资源
    最近更新 更多