0

Beginner here. I have a simple pojo (Pedido) with one property (referencia) besides the id.

Using the form below, I can change pedido.referencia through the Input Text. editListPedidoBean.salvar simply prints my List<Pedido>, and I can see that the value I typed into the InputText is there.

<h:form>
    <ui:repeat value="#{editListPedidoBean.listaPedidos}" var="pedido">
        <h:outputLabel value="#{pedido.id}" />
        <h:inputText value="#{pedido.referencia}" />
        <h:commandButton value="salvar" action="#{editListPedidoBean.salvar}" />
        <br />
    </ui:repeat>
</h:form>

However, using a dataTable, the value I type into the Input Text is not passed through to my backing bean. editListPedidoBean.salvar prints the list wiith referencia's old value.

<h:form>
    <h:dataTable value="#{editListPedidoBean.listaPedidos}" var="pedido">
        <h:column>
            <h:outputLabel value="#{pedido.id}" />
        </h:column>
        <h:column>
            <h:inputText value="#{pedido.referencia}" />
        </h:column>
        <h:column>
            <h:commandButton value="salvar"
                action="#{editListPedidoBean.salvar}" />
        </h:column>
    </h:dataTable>
</h:form>

How can I effectively bind a property in my bean to a field in a dataTable?

EDIT: Here's my bean:

package pedido;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.enterprise.inject.Model;
import javax.inject.Inject;

@Model
public class EditListPedidoBean implements Serializable {

private List<Pedido> listaPedidos = new ArrayList<>();

@Inject
EditListPedidoDao dao;

public List<Pedido> getListaPedidos() {
    listaPedidos = dao.getListaPedidos();
    return listaPedidos;
}

public void setListaPedidos(List<Pedido> listaPedidos) {
    this.listaPedidos = listaPedidos;
}

public String salvar() {
    System.out.println(listaPedidos);//I'm just printing out the list to see if my bean can 'see' the changes made in the InputText before I try to persist it.
    return null;
}

}

bernardo.g
  • 826
  • 1
  • 12
  • 27
  • Please show your bean (in [mcve] flavour) and all should work: https://stackoverflow.com/questions/355614/datatable-containing-inputtext-is-it-possible-with-jsf-custom-component. Keep in mind that not doing ajax, each row has a button which update ALL input fields in all rows. – Kukeltje Sep 12 '18 at 21:01
  • @Kukeltje Added the bean code. I understand, unfortunately I still can't bind a simple property in a dataTable... – bernardo.g Sep 12 '18 at 21:56
  • And adding the bean made it all clear. See #4 in https://stackoverflow.com/questions/2118656/commandbutton-commandlink-ajax-action-listener-method-not-invoked-or-input-value. And you also have a 'performance problem' then, see https://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times – Kukeltje Sep 13 '18 at 06:46
  • (additional remark: using `@Model` makes it request scoped, so you need to change two things) – Kukeltje Sep 13 '18 at 07:19
  • `@ViewScoped` + `@PostConstruct` did the job. Thank you for the help! – bernardo.g Sep 13 '18 at 11:46

0 Answers0