3

I am creating forms dynamically from bean. I want to bind the dynamically created components to a list of DTO's i.e a first inputtext value must be saved as a DTO object i.e List[0] , second inputtext value must be saved as a DTO object i.e List[1] , third inputtext value must be saved as a DTO object i.e List[2] .

bean

public void addInputText(String value, HtmlPanelGrid grid,
        FundamentsAttributesDBDTO attribute) {
    HtmlInputText inputText = new HtmlInputText();
    inputText.setValue(value);
    inputText.setStyleClass("tile");
    inputText.setValueExpression(
            "value",
            createValueExpression("#{formGeneratorBean.input}",
                    String.class));
    grid.getChildren().add(inputText);
}
public static ValueExpression createValueExpression(String valueExpression,
            Class<?> valueType) {
        FacesContext context = FacesContext.getCurrentInstance();
        return context
                .getApplication()
                .getExpressionFactory()
                .createValueExpression(context.getELContext(), valueExpression,
                        valueType);
    }

xhtml

 <h:body>
       <h:form id="form" binding="#{formGeneratorBean.form}" />
    </h:body>

the values in the created form must be filled in this list

private transient List<ValuesDTO> valuesList = new ArrayList<ValuesDTO>();

Please Help me with this issue.

sitaram chikkala
  • 171
  • 2
  • 16
  • 1
    You already give the answer yourself. `"#{formGeneratorBean.valuesList[0]}"`, `"#{formGeneratorBean.valuesList[1]}"` etc... just find a way to pass on the index. And are you sure you need java to create the inputs? No option to use xhtml? – Kukeltje Nov 28 '15 at 10:35
  • @Kukeltje The form doen't have any physical componenets , I add componenets from bean i.e java code to have a dynamic form. – sitaram chikkala Nov 28 '15 at 10:40
  • Read this. http://stackoverflow.com/questions/3510614/how-to-create-dynamic-jsf-form-fields other options possible to. Or take a look at e.g. PrimeFaces extensions dynaform. – Kukeltje Nov 28 '15 at 10:42
  • @Kukeltje I followed the second approach in the answer, I can create forms dynamically. I was just stuck in binding the values part. DynaForm just give the structure for us, the component part still needs to be hard-coded in html which contradicts my requirement. – sitaram chikkala Nov 28 '15 at 10:45
  • Yes I know, but read the last paragraph – Kukeltje Nov 28 '15 at 10:46
  • @Kukeltje yes I have read it too , but I couldn't find the perfect example for that approach. I have no previous experience with XML so I've opted for the Java approach. Please suggest me links with the example or code . Thank You. – sitaram chikkala Nov 29 '15 at 04:00
  • How can I now what for you is the prefect example? The code above is very simple 1:1convertible to xhtml. So it if you want to try, please do, we can comment on that. And did you try my suggestion in case you want to stick with java? – Kukeltje Nov 29 '15 at 09:24
  • @Kukeltje Yes I have tried your suggestion with sticking to java approach. But the values are not binding – sitaram chikkala Nov 29 '15 at 11:18

0 Answers0