I have a questionnaire form in which components (questions) are generated all programatically in my backing bean.
In form submit event I need to collect all user inputs and store them in db.
But JSF does not recognize the dymanically generated components and only finds the ones that are in my Facelets page which are my panelgrid and submit button. This is my submit() method.
public boolean submit() {
UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
UIComponent formComponent = viewRoot.findComponent("mainForm"); //form id
HtmlForm form = (HtmlForm)formComponent;
List<UIComponent> componentList = form.getChildren();
for(int p=0; p<componentList.size(); p++) {
UIComponent component = componentList.get(p);
System.out.println("The Component ID is:"+component.getId());
}
return true;
}
So does anyone know where I can hunt my components other than the method above?
This is not the right way to collect submitted values.
You should instead bind the component’s value attribute to a bean property. E.g.
This way JSF will just update the bean property the usual way.
You could make use of a
Map<String, Object>property to bring some more dynamics.See also: