Narayan Subedi
Narayan Subedi

Reputation: 1343

How to get dynamically rendered <h:inputText> value in back end in JSF

I am able to render dynamic but don't know how to get those dynamically created values in back end.

test.xhtml

<h:form>
        Insert Your Desire Number : <h:inputText value="#{bean.number}">
        <f:ajax listener="#{bean.submitAjax}" execute="@form" render="@form" event="keyup" />
        </h:inputText>
        <br></br>
        <h:outputText value="#{bean.text}" />
        <h:dataTable value="#{bean.items}" var="item">
            <h:column>
                <h:inputText value="#{item.value}" />
            </h:column>
        </h:dataTable>
        <h:commandButton value="Submit" action="#{item.submit}" />

    </h:form>

If I render 3 input boxes, and when I submit the button i get the last value only, Can someone guide me how can i

Bean.java

@ManagedBean(name="bean")
@SessionScoped
public class Bean {
private int number;
private List<Item> items;
private Item item;
//getter and setter are omitted
public void submitAjax(AjaxBehaviorEvent event)
{
    items = new ArrayList<Item>();
    for (int i = 0; i < number; i++) {
        items.add(new Item());
    }
}
}

Item.java

private String value;
//getter and setter are omitted
public void submit() {
        System.out.println("Form Value : "+value);

}

Upvotes: 0

Views: 2276

Answers (1)

BalusC
BalusC

Reputation: 1108557

Your submit() method is in the wrong place. You should put it on the managed bean, not on the entity.

Thus so,

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

with

public void submit() {
    for (Item item : items) {
        System.out.println(item.getValue());
    }
}

or more formally,

@EJB
private ItemService service;

public void submit() {
    service.save(items);
}

Upvotes: 1

Related Questions