Problem with JSF during form validation

Asked

Viewed 319 times

1

I have a problem with the animal registration form:
Follow the part of the diagram involved in the relationship regarding the form:

inserir a descrição da imagem aqui
The problem is that when I am registering an animal, I can put the name, age, sex and type of animal (which in the case is another table). Already in the Client field I can fill in with the data, but when clicking save, appears on the screen q missing fill in the client field and it was filled in.

inserir a descrição da imagem aqui

Follow the project codes:

Class Person:

package dominio.modelo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Pessoa {

    @Id
    @Column(length=14)
    private String cpf;

    @Column(length=25, nullable=false)
    protected String nome;

    @Column(length=25, nullable=false)
    private String sobrenome;

    @Column(length=9, nullable=true)
    private String telefone;

    public Pessoa() {

    }

    public Pessoa(String cpf, String nome, String sobrenome, String telefone) {
        this.cpf = cpf;
        this.nome = nome;
        this.sobrenome = sobrenome;
        this.telefone = telefone;
    }

    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 getSobrenome() {
        return sobrenome;
    }

    public void setSobrenome(String sobrenome) {
        this.sobrenome = sobrenome;
    }

    public String getTelefone() {
        return telefone;
    }

    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((cpf == null) ? 0 : cpf.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Pessoa other = (Pessoa) obj;
        if (cpf == null) {
            if (other.cpf != null)
                return false;
        } else if (!cpf.equals(other.cpf))
            return false;
        return true;
    }

}

Class Client

package dominio.modelo;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;

@Entity
public class Cliente extends Pessoa {

    @Column(length=20, nullable=true)
    private int credito;

    @OneToMany(mappedBy="cliente", cascade=CascadeType.PERSIST)
    private List<Animal> animais;

    public Cliente() {
        super();
    }

    public Cliente(String cpf, String nome, String sobrenome, String telefone) {
        super(cpf, nome, sobrenome, telefone);
    }

    public int getcredito() {
        return credito;
    }

    public void setCredito(int credito) {
        this.credito = credito;
    }

    public List<Animal> getAnimais() {
        return animais;
    }

    public void setAnimais(List<Animal> animais) {
        this.animais = animais;
    }

    @Override
    public String toString() {
        return nome;
    }

}

Class Animal

package dominio.modelo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;

@Entity
public class Animal {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ID_ANIMAL")
    @SequenceGenerator(name="ID_ANIMAL", sequenceName="SEQ_ID_ANIMAL", initialValue=1 ,allocationSize=1)
    private Integer id;

    @Column(length=25, nullable=false)
    private String nome;

    private Integer idade;

    @Column(nullable=false)
    private String sexo;

    @ManyToOne
    @JoinColumn(name="id_cliente")
    private Cliente cliente;

    @ManyToOne
    @JoinColumn(name="tipo")
    private Tipo tipo;

    public Animal() {
        super();
    }

    public Animal(Integer id, String nome, Integer idade, String sexo) {
        this.id = id;
        this.nome = nome;
        this.idade = idade;
        this.sexo = sexo;
    }

    public Integer getId() {
        return id;
    }

    public String getIdStr() {
        if (this.id == null)
            return "";

        return id.toString();
    }

    public String getNome() {
        return nome;
    }

    public String getNomeStr() {
        if (this.nome == null)
            return "";

        return nome.toString();
    }

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

    public Integer getIdade() {
        return idade;
    }

    public void setIdade(Integer idade) {
        this.idade = idade;
    }

    public String getSexo() {
        if (this.sexo == null)
            return "";

        return sexo.toString();
    }

    public String getSexoStr() {
        if (this.sexo == null)
            return "";

        return sexo.toString();
    }

    public void setSexo(String sexo) {
        this.sexo = sexo;
    }

    public Cliente getCliente() {
        return cliente;
    }

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

    public Tipo getTipo() {
        return tipo;
    }

    public void setTipo(Tipo tipo) {
        this.tipo = tipo;
    }

}

Class Type

package dominio.modelo;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;

@Entity
public class Tipo {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ID_TIPO")
    @SequenceGenerator(name="ID_TIPO", sequenceName="SEQ_ID_TIPO", initialValue=1 ,allocationSize=1)
    private Integer codigo;

    @Column(unique=true)
    private String nome;

    @OneToMany(mappedBy="tipo")
    private List<Animal> animais;

    public Tipo() {
        super();
    }

    public Tipo(String nome) {
        this.nome = nome;
    }


    public Integer getCodigo() {
        return codigo;
    }

    public String getNome() {
        return nome;
    }

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

    public List<Animal> getAnimais() {
        return animais;
    }

    public void setAnimais(List<Animal> animais) {
        this.animais = animais;
    }

    @Override
    public String toString() {
        return nome;
    }

}

Page editarAnimal.xhtml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:p="http://primefaces.org/ui"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">

<ui:composition template="modelo.xhtml">

    <ui:define name="">Edição de Animal</ui:define>

    <ui:define name="conteudo">
        <p:panelGrid columns="2">

            <ui:remove>
                <p:outputLabel for="id" value="ID:" />
                <p:inputText id="id" value="#{animalMB.animal.id}" required="true" />
            </ui:remove>

            <p:outputLabel for="nome" value="Nome:" />
            <p:inputText id="nome" value="#{animalMB.animal.nome}" required="true" />

            <p:outputLabel for="idade" value="Idade:" />
            <p:inputText id="idade" value="#{animalMB.animal.idade}" required="true" />

            <p:outputLabel for="sexo" value="Sexo:" />
            <p:selectOneMenu id="sexo" value="#{animalMB.animal.sexo}" required="true">
                <f:selectItem itemValue="" itemLabel="Selecione" />
                <f:selectItem itemValue="M" itemLabel="Macho" />
                <f:selectItem itemValue="F" itemLabel="Fêmea" />
            </p:selectOneMenu>

            <p:outputLabel for="cliente" value="Cliente:" />
            <p:selectOneMenu id="cliente" value="#{animalMB.animal.cliente}" converter="cliente-converter" required="true">
                <f:selectItem itemValue="" itemLabel="Selecione" />
                <f:selectItems value="#{animalMB.clientes}" var="cli" itemLabel="#{cli.nome}" />
            </p:selectOneMenu>

            <p:outputLabel for="tipo" value="Tipo:" />
            <p:selectOneMenu id="tipo" value="#{animalMB.animal.tipo}" converter="tipo-converter" required="true">
                <f:selectItems value="#{animalMB.tipos}" var="tipo" itemLabel="#{tipo.nome}"></f:selectItems>
            </p:selectOneMenu>

            <h:outputLabel value="" />

            <h:panelGroup>
                <p:commandLink ajax="false" value="Salvar" action="#{animalMB.acaoSalvar}" />
            -
            <p:commandLink ajax="false" immediate="true" value="Cancelar" action="#{animalMB.acaoListar}" />

            </h:panelGroup>

        </p:panelGrid>

    </ui:define>

</ui:composition>
</html>
  • Lack of property itemValue in your p:selectItems of customer and type.

  • pear, missed where? no <f:selectItems> or in the <p:selectOneMenu> ??

  • In the f:selectItems.

  • Then I have to change: <f:selectItems value="#{animalMB.clientes}" var="cli" itemLabel="#{cli.nome}" /> for <f:selectItems itemValue="#{animalMB.clientes}" var="cli" itemLabel="#{cli.nome}" /> ?

  • puts your client converter’s code

  • @Henriquesantiago follows the Clienteconverter https://ghostbin.com/paste/craw2

  • @Renannarciso man, is not charging.

  • @Henriquesantiago what is not carrying?

Show 3 more comments

1 answer

0

Lack of property itemValue in his f:selectItems, it serves to set the client in the animal object of your Managed bean animalMB. How you declared a convert and it builds the client object from a cpf then pass the property cpf in the itemValue of f:selectItems.

<p:outputLabel for="cliente" value="Cliente:" />
<p:selectOneMenu id="cliente" value="#{animalMB.animal.cliente}" converter="cliente-converter" required="true">
    <f:selectItem itemValue="" itemLabel="Selecione" />
    <f:selectItems value="#{animalMB.clientes}" var="cli" itemValue="#{cli.cpf}" itemLabel="#{cli.nome}"/>
</p:selectOneMenu>
  • I did what was asked and the fault still persists.

  • Does any error appear besides interface error? You can post Animalmb and client-convert code?

  • I put a Try/catch in the save method to see the e. printstacktrace and show me some error in the log, but n appeared... Animalmb - https://ghostbin.com/paste/gbfhz Clienteconverter - https://ghostbin.com/paste/craw2

  • @Renannarciso, I changed in my answer. Beyond the itemValue that was missing I did not notice that you were using a convert to customer, that convert expects a cpf to assemble the client object so I modified my response.

  • Man, I did what was asked. I put the property itemValue="#{cli.cpf}" and the error persists, no saving... Keeps giving validation error: Client: Validation error: the value is required.

Browser other questions tagged

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