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

时间:2015-06-12 17:55:10

标签: ajax jsf richfaces ajax4jsf

每行都有一个带h:selectOneMenu的数据表。我希望能够检索bean中selectOneMenu中选择的值。我正在使用richfaces a4j:support标签来对辅助bean进行AJAX调用。您可以看到以下代码:

DataTable标头:

public class MainMenu implements Scene {

   Background background;
   Hero hero;  
   Enemy enemy; 
   MenuButtons buttons

    public mainMenu(Background background, Hero hero,  Enemy enemy, MenuButtons buttons){

       this.background = background;
       this.hero = hero;
       this.enemy = enemy;
       this.buttons = buttons;   
    }

    @Override
    public void render(){

        this.background.draw();
        this.hero.draw();
        this.enemy.draw();
        this.mainMenuButtons.draw();           

    }

    @Override
    public void updateLogic(){

        this.hero.move();
        this.enemy.move();
        this.mainMenubuttons.animate();
    }

}

SelectOneMenu with A4j:

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

要执行的Backing Bean方法:

<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>
  • comboBox .getValue()返回NULL,即使我选择了一个值。

PS:

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

1 个答案:

答案 0 :(得分:4)

确定了问题:

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

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

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

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

相关问题