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

Asked

Viewed 318 times

1

Taking into account 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 controller :

@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 jsf form that had a field where I could enter a phone list? I’ve never done anything like!!

  • 1

    You can create an input with a button on the front, every time you fill the phone and click the button it add the value in the list that is in your Controller.

  • @Diegoaugusto Could you explain to me how through a partner response?

  • 1

    I’m sorry, but I’m a little sharp at the moment. Basically it would be an input with an ADD button, every time you click the add vc pushes in the list. That is, you need to create a method that when you click the ADD vc passes the value of the input and add it to the list.

  • 1

    @Thank you for your attention I’ll see if I can do it!!

  • 1

    For nothing, if ngm reply later I try to draft a reply.

1 answer

1

Buddy, you need a view, an acquittal. XHTML 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 must follow the hierarchy of the XHML file, as in HTML files;

Follow the basic template of a XHTML file, I won’t do it for you, if it doesn’t get old.

  • 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 input label
  • The <h:inputText></h:inputText>, is your inbox.
  • The "value" of imput text is where you will perform Binding 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’ve helped you;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.