Search code examples
ajaxjsfjsf-2facelets

Text box Rendering in JSF 2.0


I have a text box where the end user has to enter the number of text boxes that should be rendered. Based on the number entered, the number of text boxes have to render dynamically in row-format.

Also in the newly rendered row formatted text boxes the values entered has to be identified with correct backing bean when it goes to the next page.

Any suggestions on where to start or how to achieve something like this?


Solution

  • You can use <h:inputText> to ask enduser for input.

    <h:inputText value="#{bean.number}" />
    

    You can use <h:commandButton> to let user submit a form.

    <h:commandButton value="Submit" action="#{bean.submit}" />
    

    You can use List<T> to have a collection of items.

    private List<Item> items;
    
    public void submit() {
        items = new ArrayList<Item>();
    
        for (int i = 0; i < number; i++) {
            items.add(new Item());
        }
    }
    

    You can use <h:dataTable> to present it in rows.

    <h:dataTable value="#{bean.items}" var="item">
        <h:column>
            <h:inputText value="#{item.value}" />
        </h:column>
    </h:dataTable>
    

    The submitted values will just end up right there in items.