How can I retrieve the submitted values of a dynamically generated form?

2.4k views Asked by At

I need to do an application that allows the final user to create his own form (drop checkboxes,inputTexts, etc) save that form and then the user can open the form and write values in the elements of the form for then print the form. I'm working with icefaces, and I was doing something in the managed bean:

javax.faces.component.UIViewRoot root = FacesContext.getCurrentInstance().getViewRoot();
            // find the form with its clientId.
            UIComponent a = root.findComponent("a");
            UIComponent b = a.findComponent("form");
            UIComponent form = b.findComponent("formulario");
            List children = form.getChildren();

if (tipo.equalsIgnoreCase("text")) {
                HtmlInputText inputText = new HtmlInputText();
                inputText.setId("text" + nro + (indice + 1));
                indice++;
                inputText.setValue("");
                inputText.setSize(tamanio);
                children.add(inputText);
                elemento = new Elemento(tipo, inputText.getId(), inputText.getValue().toString(), inputText.getSize());
            } else if (tipo.equalsIgnoreCase("textarea")) {
                HtmlInputTextarea inputTextArea = new HtmlInputTextarea();
                inputTextArea.setId("textarea" + nro + (indice + 1));
                indice++;
                inputTextArea.setValue("");
                inputTextArea.setCols(ancho);
                inputTextArea.setRows(alto);
                children.add(inputTextArea);
                elemento = new Elemento(tipo, inputTextArea.getId(), inputTextArea.getValue().toString(), (inputTextArea.getRows() * 10) + inputTextArea.getCols());
            } else if (tipo.equalsIgnoreCase("outputText")) {
                HtmlOutputText outputText = new HtmlOutputText();
                outputText.setId("outputText" + nro + (indice + 1));
                indice++;
                outputText.setValue(valor);
                children.add(outputText);
                elemento = new Elemento(tipo, outputText.getId(), outputText.getValue().toString(), 0);
            } else if (tipo.equalsIgnoreCase("table")) {
                HtmlDataTable table = new HtmlDataTable();
                table.setId("table" + nro + (indice + 1));
                indice++;
                // for (int k = 0; k < filas; k++) {
                for (int j = 0; j < columnas; j++) {
                    HtmlColumn column = new HtmlColumn();
                    column.setId("c" + j);
                    for (int i = 0; i < filas; i++) {
                        HtmlInputText inputColumn = new HtmlInputText();
                        inputColumn.setId("f" + i);
                        inputColumn.setValue("");
                        column.getChildren().add(inputColumn);
                    }
                    table.getChildren().add(column);
                }
                // }

                table.setValue("");
                table.setRows(filas);
                table.setColNumber(columnas);
                table.setResizable(true);
                children.add(table);
                elemento = new Elemento(tipo, table.getId(), table.getValue().toString(), (table.getRows() * 10 + table.getColNumber()));
            } else if (tipo.equalsIgnoreCase("checkbox")) {
                HtmlSelectBooleanCheckbox checkbox = new HtmlSelectBooleanCheckbox();
                checkbox.setId("checkbox" + nro + (indice + 1));
                indice++;
                checkbox.setValue("ON");
                children.add(checkbox);
                elemento = new Elemento(tipo, checkbox.getId(), checkbox.getValue().toString(), 0);
            } else if (tipo.equalsIgnoreCase("radio")) {
                HtmlSelectOneRadio radio = new HtmlSelectOneRadio();
                radio.setId("radio" + nro + (indice + 1));
                indice++;
                UISelectItems items = new UISelectItems();
                ArrayList arr = new ArrayList();
                HtmlInputText inputRadio = new HtmlInputText();
                inputRadio.setValue(" ");
                arr.add(new SelectItem(new Integer(0), inputRadio.getValue().toString()));
                items.setValue(arr);
                radio.setValue("ON");
                radio.getChildren().add(items);
                children.add(radio);
                elemento = new Elemento(tipo, radio.getId(), radio.getValue().toString(), 0);
            } else if (tipo.equalsIgnoreCase("panel")) {
                HtmlPanelGrid panel = new HtmlPanelGrid();
                panel.setId("panel" + nro + (indice + 1));
                indice++;
                children.add(panel);
                elemento = new Elemento(tipo, panel.getId(), "", 0);
            }
            elementosFormulario.add(elemento);

I can add the elements in the form then I save in the DB then I can recreate the form but then I don't know how to retrieve the submitted values from every element.

1

There are 1 answers

0
BalusC On BEST ANSWER

Bind the values to a Map<String, Object> property.

E.g.

private Map<String, Object> values = new HashMap<String, Object>(); // +getter

with

inputText.setValueExpression("value", createValueExpression("#{bean.values['" + field.getName() + "']}", String.class));

You can then obtain the submitted values in action method by values.get(field.getName()).

See also: