JSF 1.2生命周期理解:在InvokeApplication阶段执行ValueChangeListener方法 [英] JSF 1.2 Life Cycle understanding: Executing the ValueChangeListener method in InvokeApplication phase

查看:144
本文介绍了JSF 1.2生命周期理解:在InvokeApplication阶段执行ValueChangeListener方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DataTable的方面标题中使用了<h:selectBooleanCheckbox>.该<h:column>数据表的所有行内容均为<h:selectBooleanCheckbox>.这些行已按照我想要的方式进行了完美选择.下面是我使用的代码:

I am using a <h:selectBooleanCheckbox> in the facet header of a DataTable. All the rows content for that <h:column> DataTable are <h:selectBooleanCheckbox>. The rows are getting selected perfectly the way I wanted. Below is the code I used:

<h:form>
<h:dataTable
    value="#{employeeService.employeeList }"
    var="empl"
    binding="#{employeeService.dataTablebinding }">
    ......
    ......
    ......
    <h:column>
            <f:facet name="header">             
                <h:selectBooleanCheckbox id="chkBoxAll" value="#{employeeService.checkedHdr }" valueChangeListener="#{employeeService.checkAll }" onclick="submit()"></h:selectBooleanCheckbox>
            </f:facet>
            <h:selectBooleanCheckbox id="tableChkBox" value="#{empl.checked }" valueChangeListener="#{employeeService.getCheckChanged }" onclick="submit()"></h:selectBooleanCheckbox>
        </h:column>
</h:dataTable>

这是ValueChangeListener的代码:

public void checkAll(ValueChangeEvent event){
    if(isInvokeApplicationPhase(event)){
        Iterator<Employee> empl = employeeList.iterator();
        while(empl.hasNext()){
            Employee emp = empl.next();             
            emp.setChecked(checkedHdr);
        }
    }
}

这是我为使此代码正常工作而添加的isInvokeApplicationPhase实用程序(指的是BalusC在此链接中建议的解决方案:

This is the isInvokeApplicationPhase utility I added up for making this code work (referred the solution suggested by BalusC in this link: JSF 1.2: valueChangeListener event not returning newly selected value ):

public boolean isInvokeApplicationPhase(FacesEvent event){      
    if(event.getPhaseId() != PhaseId.INVOKE_APPLICATION){
        event.setPhaseId(PhaseId.INVOKE_APPLICATION);
        event.queue();
        return false;
    }       
    return true;
}

现在我的问题:

ValueChangeListener方法中该isInvokeApplicationPhase检查的用途是什么?如果我注释掉该支票,那么它将不起作用-为什么?我以为我已经正确理解了JSF生命周期,但是这种行为证明我还没有:(

What is the use of that isInvokeApplicationPhase check in the ValueChangeListener method? If I comment out this check then it does not work - WHY? I thought I have understood the JSF life cycle properly but this behavior proves that I have not :(

请让我知道基于JSF生命周期阶段的解释.

Kindly let me know the explanation based on the JSF life cycle phases.

推荐答案

值更改侦听器在验证阶段运行,旨在挂钩值更改事件. IE.新提交的值与原始模型值不同.

The value change listener runs during validations phase and is intented to hook on the value change event. I.e. the newly submitted value is different from the original model value.

验证阶段在更新模型值阶段之前运行,其中JSF设置模型中所有提交,转换和验证的值(支持bean的属性).因此,当您尝试在值更改侦听器中更改其他字段的模型值时,它将在更新模型值阶段被覆盖.

The validations phase runs before update model values phase, wherein JSF set all submitted, converted and validated values in the model (the backing bean's properties). So when you attempt to change the model value of a different field in the value change listener, then it would be overridden during the update model values phase.

此特殊技巧将值更改事件重新排队到调用应用程序阶段,该阶段在 更新模型值阶段之后运行.因此,当您尝试更改其他字段的模型值时,它将不会被覆盖.

This particular trick re-queues the value change event to the invoke application phase, which runs after update model values phase. So when you attempt to change the model value of a different field, then it won't be overriden.

简而言之:验证阶段仅仅是手动操作模型值的错误时刻,而调用应用程序阶段正是手动操作模型值的正确时刻.

In a nutshell: the validations phase is simply the wrong moment to manually manipulate the model values and the invoke application phase is the right moment to manually manipulate the model values.

实际上,这里价值滥用事件被滥用.您宁可在这里使用一个动作事件,但这不适用于JSF 1.x中的输入.仅在<f:ajax listener>风格的JSF 2.x中可用.

Actually, the value change event is being abused here. You should rather be using an action event here, but this was not available for inputs in JSF 1.x. This was only available in JSF 2.x in flavor of <f:ajax listener>.

这篇关于JSF 1.2生命周期理解:在InvokeApplication阶段执行ValueChangeListener方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆