1
resposta

Property [autores] not found on type [br.com.caelum.livraria.bean.LivroBean]

Estou recebendo um 500 ao tentar acessar http://localhost:8080/livraria/livro.xhtml.

Pelo o que entendi, a propriedade autores não foi encontrada no LivroBean.

Segue minha classe LivroBean

package br.com.caelum.livraria.bean;

import java.util.List;
import javax.faces.bean.ManagedBean;
import br.com.caelum.livraria.dao.DAO;
import br.com.caelum.livraria.modelo.Autor;
import br.com.caelum.livraria.modelo.Livro;

@ManagedBean(name = "livroBean")
public class LivroBean {

    private Livro livro = new Livro();
    private Integer autorId;

    public Integer getAutorId() {
        return autorId;
    }

    public void setAutorId(Integer autorId) {
        this.autorId = autorId;
    }

    public Livro getLivro() {
        return livro;
    }

    public List<Autor> getAutores(){
        return new DAO<Autor>(Autor.class).listaTodos();
    }

    public void gravar() {
        System.out.println("Gravando livro " + this.livro.getTitulo());

        if (livro.getAutores().isEmpty()) {
            throw new RuntimeException("Livro deve ter pelo menos um Autor.");
        }

        new DAO<Livro>(Livro.class).adiciona(this.livro);
    }

    public void gravarAutor() {
        Autor autor = new DAO<Autor>(Autor.class).buscaPorId(this.autorId);
        this.livro.adicionaAutor(autor);
    }

}

Segue meu livro.xhtml

<?xml version="1.0" encoding="UTF-8" ?>
<!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:h="http://java.sun.com/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core">

<h:body>
    <h1>Novo Livro</h1>
    <h:form>
        <fieldset>
            <legend>Dados do Livro</legend>
            <h:panelGrid columns="2">

                <h:outputLabel value="Titulo:" for="titulo" />
                <h:inputText id="titulo" value="#{livroBean.livro.titulo}" />

                <h:outputLabel value="ISBN:" for="isbn" />
                <h:inputText id="isbn" value="#{livroBean.livro.isbn}" />

                <h:outputLabel value="Preço:" for="preco" />
                <h:inputText id="preco" value="#{livroBean.livro.preco}" />

                <h:outputLabel value="Data de Lançamento:" for="dataLancamento" />
                <h:inputText id="dataLancamento" value="#{livroBean.livro.dataLancamento}" />

            </h:panelGrid>
        </fieldset>

        <fieldset>
            <legend>Dados do Autor</legend>
            <h:outputLabel value="Selecione Autor: " for="autor"/>
            <h:selectOneMenu value="#{livroBean.autorId}" id="autor">
                <f:selectItems value="#{livroBean.autores}" var="autor" itemLabel="#{autor.nome}" itemValue="#{autor.id}"/>
            </h:selectOneMenu>
            <h:commandButton value="Gravar Autor" action="#{livroBean.gravarAutor}"/>
        </fieldset>

        <h:commandButton value="Gravar" action="#{livroBean.gravar}" />
    </h:form>
</h:body>

</html>

Desde já, agradeço a atenção.

1 resposta

Oi DIEGO

Property [autores] not found on type [br.com.caelum.livraria.bean.LivroBean]

Tem a stack completa desse erro para enviar? Não identifiquei nenhum problema no código que você enviou.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software