调用ajax事件侦听器之前未更新Bean值

时间:2015-05-01 11:10:27

标签: ajax jsf icefaces

我在实现ajax侦听器事件时遇到一些问题,该事件检测表单上的日期何时更改。我有一个数据表,在其中一个列中我有一个<ace:dateTimeEntry>,它包含一个存储在bean中的开始日期字段。 (注意:联盟是用于数据表的变量的名称)。

<ace:column headerText="Start Date" rendered="#{not alliance.deletePending}">
    <ace:dateTimeEntry id="startDateField" value="#{alliance.allianceStartDate}" pattern="dd/MMM/yyyy" renderAsPopup="true" effect="fadeIn">
        <ace:ajax execute="@this" render="@this" event="dateSelect" listener="#{allianceViewBean.changeAllianceActiveIndicator}"/>
        <ace:ajax execute="@this" render="@this" event="dateTextChange" listener="#{allianceViewBean.changeAllianceActiveIndicator}"/>
    </ace:dateTimeEntry>
</ace:column>

我正在使用一个标记,该标记在bean中调用一个名为

的侦听器方法
#{allianceViewBean.changeAllianceActiveIndicator}

这是bean值更改方法:

public void changeAllianceActiveIndicator(AjaxBehaviorEvent event) {
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

    Calendar c = Calendar.getInstance();
    c.setTimeZone(TimeZone.getTimeZone("UTC"));

    java.util.Date currentDate = c.getTime();

    for (AllianceBean bean : carrierAllianceDetails) {
        if ((bean.getAllianceStartDate().compareTo(currentDate) < 0)
            && (bean.getAllianceEndDate().compareTo(currentDate) > 0)) {
            bean.setActive(true);
        } else {
            bean.setActive(false);
        }
    }
}

但是,当我调试此方法时会发生错误。侦听器正确到达方法,但bean中的开始日期值不是更新值,它仍然引用更改前的旧值。如果我在表单上输入新值,则bean中的值始终引用先前输入的日期值。方法中的逻辑是正确的,但是检查的值不是。

我不确定如何确保侦听器方法从表单中获取最新值。

由于

1 个答案:

答案 0 :(得分:0)

这是由于您所处的生命周期阶段。检查此事件进入的生命周期阶段。如果它在UPDATE_MODEL_VALUES阶段之前(例如在PROCESS_VALIDATIONS阶段),则您的bean值尚未更新。在这种情况下,我建议在事件上将phaseId设置为INVOKE_APPLICATION,对其进行排队并返回方法:

public void changeAllianceActiveIndicator(AjaxBehaviorEvent event) {
    if (!event.getPhaseId().equals(PhaseId.INVOKE_APPLICATION) {
        event.setPhaseId(PhaseId.INVOKE_APPLICATION);
        event.queue();
        return;
    }

    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

    Calendar c = Calendar.getInstance();
    c.setTimeZone(TimeZone.getTimeZone("UTC"));

    java.util.Date currentDate = c.getTime();

    for (AllianceBean bean : carrierAllianceDetails) {
        if ((bean.getAllianceStartDate().compareTo(currentDate) < 0)
            && (bean.getAllianceEndDate().compareTo(currentDate) > 0)) {
            bean.setActive(true);
        } else {
            bean.setActive(false);
        }
    }
}

除此之外,您的方法的代码也不理想。设置默认timeZone已经过时 - 以及整个Calendar块。你可以做到

Date currentDate = new Date();

我还建议将bean上设置的日期转换为服务器timeZone的日期 - 否则你要比较苹果和橘子。

相关问题