2017-07-19 223 views
1

我对Primefaces的工作逻辑还有一个误解。Primefaces日历没有设置日期

现在<p:calendar>当我按下提交按钮时,组件不能设置选定的日期。
我看到它已成功进入actionListener方法,但日期值为空。

首先,我尝试使用标准PF example创建我的日历。它看起来太简单了,我认为在用户选择日期或提交表单时,组件应该调用值设置器。但是它在第一个和第二个案例中都没有做到。 好了,我打开谷歌和发现了几个帖子:

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。

之后我移动<p:calendar><p:commandButton>部件到相同<h:form>并试图两个不同的过程值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。

+0

该代码会引发一些错误? –

+0

@PolRodríguezno。它不是。我查了NetBeans输出,发现只有Fine和Finest的消息有'组件创建' –

+0

顺便说一下,我也检查了位于同一页面的输入字段是否正确提交。我可以在调试模式下看到java字段具有正确的值。 –

回答

0

试图与我有一个代码,一个比喻,我认为把按钮进入相同的形式日历,并省略了“过程”和“partialSubmit”应该工作:

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

我希望它有帮助!

+0

嗨!感谢您的回复。我试图做到这一点,但不幸的是它没有帮助。 所有inputText字段和选择菜单传递值,但日历不是:( –

+0

你应该在上面的代码中省略valueChangeListener,我在我的回复中跳过了这个细节 – imnothatrobot

+0

那么,现在我删除了valueChangeListener并返回到初始教程配置。但是我不能删除'process'参数,否则form甚至不会将inputText字段值传递到托管bean。 –

0

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); 
    } 
} 

希望这有助于!

+0

我仍然无法理解事件模型的概念,我会调查它,但它确实看起来很奇怪。日期可以简单地通过在java后端具有日期类型的值参数传递 –

+0

您是否获得日历事件保存工作? –

+0

现在我不需要手动处理日历事件了,对于我的目标来说,这已经足够了选择日期并通过提交将其传递给托管bean按钮。正如我已经写了,我发现了错误,并可以使用默认的方式来设置日期。 –

2

好吧,伙计们,我发现我的错误。愚蠢的错误。又一次重新阅读PF文档显示,使用readonly参数对我的目标不正确。我想防止手动日期输入直接进入<p:calendar>文本字段。但是根据文档: readonly -

标志,指示该输入元素将防止由 用户

变化,但我需要readonlyInput参数,

使的输入文本弹出日历只读。

因此,第二个参数可以防止输入,而第一个参数可以防止输入完全变化。
谢谢你的帮助。