How to make a form with JSF with a field that can have as many values as needed?

1

Considering the following entity class:

import java.io.Serializable;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;



@Entity
@SequenceGenerator(name = "cliente_sequencia", sequenceName = "cliente_sequencia",
        allocationSize = 1, initialValue = 1)
public class Cliente implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "cliente_sequencia")
    private int id;
    private String cpf;  
    private String nome;
    private String email;  
    private List<String> telefones;   

    public Cliente(){
    }

    public Cliente(String cpf, String nome, String email, List<String> telefones) {
        this.cpf = cpf;
        this.nome = nome;
        this.email = email;
        this.telefones = telefones;
    }

    public int getId() {
        return id;
    }


    public String getCpf() {
        return cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public List<String> getTelefones() {
        return telefones;
    }

    public void setTelefones(List<String> telefones) {
        this.telefones = telefones;
    }
    public void addTelefone(String telefone) {
        telefones.add(telefone);
    }
    public void removeTelefone(String telefone) {
        telefones.add(telefone);
    }

    @Override
    public String toString() {
        return "Cliente{" + "id=" + id + ", cpf=" + cpf + ", nome=" + nome + ", email=" + email + ", telefones=" + telefones + '}';
    }

}  

Having this driver:

@Named
@RequestScoped
public class Controlador2 implements Serializable{

    private Cliente cliente =  new Cliente(); 
    @EJB
    private ClienteDAO servico = new ClienteDAO();

    private List<Cliente> todos = new ArrayList<>();

    public String redirecionar(){

        return "index.xhtml";
    }
    public String salvar(){
        servico.salvar(cliente);
        todos = servico.todos();
        return "listaTodos.xhtml";
    }

    public Cliente getCliente() {
        return cliente;
    }

    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    public List<Cliente> todos() {
        return servico.todos();
    }

    public void setTodos(List<Cliente> todos) {
        this.todos = todos;
    }


}

How could I create a form with jsf that had a field where I could insert a list of phones? I've never done anything like that !!

    
asked by anonymous 22.08.2016 / 20:59

1 answer

1

Friend, you need a View, a .XHTML file which will contain the code of the form in question, is usually created and allocated in the WebContent folder of your Web project. Remember, you should follow the hierarchy of the XHML file, as in HTML files;

The basic template of an XHTML file, I will not do it for you, if you do not lose the grace.

  • The <h:body></h:body> , is the body;
  • The <h:form ></h:form> , the form
  • The <h:outputLabel></h:outputLabel> , is what will be shown, the entry label
  • The <h:inputText></h:inputText> , is your inbox.
  • The value of the imput text is where you will bind with your controller (named bean).

        

<h:body>
   <h:form > 
      <h:panelGrid>
        <h:outputLabel value="campo 01"/>
        <h:inputText id="campo01" value=""/>

        <h:outputLabel value="campo 02"/>
        <h:inputText id="campo02" value=""/>

        <h:outputLabel value="campo 03"/>
        <h:inputText id="campo03" value=""/>

        <h:outputLabel value="campo n"/>
        <h:inputText id="campoN" value=""/>

      </h:panelGrid>
   </h:form>
</h:body>
</html>

ps: study the official documentation.

I hope I have helped you;

    
25.08.2016 / 21:11