ajax – a4j:support – 从h检索的值:selectOneMenu始终为NULL

前端之家收集整理的这篇文章主要介绍了ajax – a4j:support – 从h检索的值:selectOneMenu始终为NULL前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
每行都有一个带h:selectOneMenu的数据表.我希望能够检索bean中selectOneMenu中选择的值.我正在使用richfaces a4j:support标签来对辅助bean进行 AJAX调用.你可以看到下面的代码

DataTable标头:

<t:dataTable id="datatable" var="row" value="#{myBean.dataTableRows}">

SelectOneMenu with A4j:

<h:selectOneMenu id="type" label="Type:" styleClass="tamanho80" 
                                value="#{datatableHolder.selectedValue}" converter="comboConverter" immediate="true" >                           
  <f:selectItem itemValue="#{null}" itemLabel="" />
  <t:selectItems var="tp" 
    itemValue="#{tp}" itemLabel="#{tp.nome}"
        value="#{row.comboTypeValues}"  />
   <f:attribute name="row" value="#{row}"/>                                                         
   <a4j:support event="onchange" reRender="parent" actionListener="${myBean.executeAjax}" immediate="true" ajaxSingle="true" />
</h:selectOneMenu>

要执行的Backing Bean方法

public void executeAjax(ActionEvent event){

    ValueHolder comboBox = (ValueHolder) event.getComponent().getParent();      
    comboBox .getValue();

}

> comboBox .getValue()返回NULL,即使我选择了一个值.

PS:

这个问题已被确定为this question的可能重复,但事实并非如此.我的问题使用dataTable并且不对每个元素使用绑定.另外,我使用的是JSF 1.1和RichFaces 3.3.3.

解决方法

确定了问题:

由t:selectItems标记生成的每个“选项”都使用项目ID而不是索引,而comboConverter使用索引来选择项目.因此,列表有12个项目(索引的范围应为0到11),但所选项目的ID为22.然后转换器将遍历列表到索引22并检索该元素.但是这个列表中没有这样的索引,因为最大值是12,然后转换器将始终返回NULL.

对于这个问题,基本上有3种方法可以解决

>创建一个新的转换器,通过它的id查找该项目
>调整/更改“comboConverter”以通过它的id查找项目(这样做会影响使用此转换器的其他代码
>调整列表以使用索引而不是ID

由于系统中的轻微影响,我选择了第一个.

猜你在找的Ajax相关文章