Search code examples
jsfdynamicjsf-2components

Dynamically create components twice


I'm trying to create via programmcode a HtmlSelectOneMenu. This is not the problem. I realised it like this:

final UISelectItem select = (UISelectItem) app.createComponent(UISelectItem.COMPONENT_TYPE);

//Items für DropDownMenü erstellen
List<SelectItem> items = new ArrayList<SelectItem>();
for (int k = 0; k < Gender2.values().length; k++){
    items.add(new SelectItem(Gender2.values()[k]));                
}

//Items dem DropDown Menü zuordnen
UISelectItems selectItems = new UISelectItems();
selectItems.setValue(items);
selectOneRadio = (HtmlSelectOneMenu) app.createComponent(HtmlSelectOneMenu.COMPONENT_TYPE);
selectOneRadio.getChildren().add(selectItems);
testgrid.getChildren().add(selectOneRadio);

When I call this method again, the following error appears: Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception javax.servlet.ServletException: Komponenten-ID j_idt3:j_id5 was already found

Want can I do against this error?


Solution

  • You should assign an unique IDs to components that you are creating with setId() method.

    You can ether generate id's on your own, or use:

    FacesContext.getCurrentInstance().getViewRoot().createUniqueId()
    

    to generate them.