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!!
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
@Diegoaugusto Could you explain to me how through a partner response?
– Pena Pintada
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.
– DiegoAugusto
@Thank you for your attention I’ll see if I can do it!!
– Pena Pintada
For nothing, if ngm reply later I try to draft a reply.
– DiegoAugusto