从 Backing bean 向 JSF 页面动态添加一个新的 p:SelectOneMenu

Add a new p:SelectOneMenu to a JSF page dynamically from the Backing bean

我想在这里

向我的 panelGrid 动态添加一个新的 p:SelectOneMenu
<h:panelGrid id="panel_grid" columns="1" style="margin-bottom:10px"cellpadding="5" >

   <p:outputLabel for="firstQuestionDropDown" value={InsererAffaireController.firstQuestion.libeleQuestion}"
   <p:selectOneMenu id="firstQuestionDropDown" value="#{InsererAffaireController.firstQuestion.libeleQuestion}" style="width:150px">
      <p:ajax listener="#{InsererAffaireController.displayNextQuestion()}" update="panel_grid" />
      <f:selectItems value="#{InsererAffaireController.firstQuestion.listLibeleReponses}" />
    </p:selectOneMenu>

            <!-- Here i want to add my new SelectOneMenu -->    
</h:panelGrid>

我设法从托管 bean 做到了,但问题是我无法填充新的 SelectOneMenu,而且我不知道如何向其添加 f:selectItems 这是将新 p:SelectOneMenu 添加到面板网格的方法,但它仍然是空的

    public void displayNextQuestion() {
       nextQuestion = getNextQuestion();
       OutputLabel nextQuestionLabelUI = new OutputLabel();
       nextQuestionLabelUI.setValue(nextQuestion.getLibeleQuestion());

       SelectOneMenu nextQuestionDropDown = new SelectOneMenu() ;

       List<SelectItem> selectItems = new ArrayList<>();

       for (String s : nextQuestion.getListLibeleReponses()) {

        selectItems.add(new SelectItem(s, s));
        System.out.println(selectItems.get(0).getLabel());
       }
       // How can i add the selectitems to the SelectOneMenu
       UIComponent panelGrid = findComponent("panel_grid");
       panelGrid.getChildren().add(nextQuestionLabelUI);
       panelGrid.getChildren().add(nextQuestionDropDown) ;
       RequestContext.getCurrentInstance().update("panel_grid");

}

您可以像这样使用 UISelectItems class:

...
UISelectItems selectItemsComponent = new UISelectItems();
selectItemsComponent.setValue(selectItems);
nextQuestionDropDown.getChildren().add(selectItemsComponent);
...