3
respostas

Erro no #{livroBean.autores}

Quando tento acessar a aplicação lança o seguinte:

E: Error Rendering View[/livro.xhtml]
javax.el.ELException: /livro.xhtml @36,77 value="#{livroBean.autores}": Error reading [autores] on type [br.com.caelum.livraria.bean.LivroBean]

Não estou conseguindo localizar qual seria o problema no código para a ocorrência do erro. Deixo abaixo o livro.xhtml e o LivroBean:

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://java.sun.com/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}">
                    <f:selectItems value="#{livroBean.autores}" var="autor"
                        itemLabel="#{autor.nome}" itemValue="#{autor.id}" />
                </h:selectOneMenu>
                <h:commandButton value="Gravar Autor"
                    action="#{livroBean.gravarAutor}" />

                <h:dataTable value="#{livroBean.autoresDoLivro}" var="autor">
                    <h:column>
                        <h:outputText value="#{autor.nome}" />
                    </h:column>
                </h:dataTable>
            </fieldset>

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

        </h:form>

        <h:dataTable value="#{livroBean.livros}" var="livro">
            <h:column>
                <f:facet name="header">Titulo</f:facet>
                <h:outputText value="#{livro.titulo}" />
            </h:column>
            <h:column>
                <f:facet name="header">ISBN</f:facet>
                <h:outputText value="#{livro.isbn}" />
            </h:column>
            <h:column>
                <f:facet name="header">Preço</f:facet>
                <h:outputText value="#{livro.preco}" />
            </h:column>
        </h:dataTable>

    </h:body>
</html>

LivroBean

package br.com.caelum.livraria.bean;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import br.com.caelum.livraria.dao.DAO;
import br.com.caelum.livraria.modelo.Autor;
import br.com.caelum.livraria.modelo.Livro;

@ManagedBean
@ViewScoped
public class LivroBean implements Serializable {

    private static final long serialVersionUID = 1L;

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

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

    public Integer getAutorId() {
        return autorId;
    }

    public Livro getLivro() {
        return livro;
    }

    public List<Livro> getLivros() {
        return new DAO<Livro>(Livro.class).listaTodos();
    }

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

    public List<Autor> getAutoresDoLivro() {
        return this.livro.getAutores();
    }

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

    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);
        this.livro = new Livro();
    }
}
3 respostas

Oi André, tem mais alguma mensagem de erro além dessa "javax.el.ELException: /livro.xhtml @36,59 value="#{livroBean.autores}": Error reading [autores] on type [br.com.caelum.livraria.bean.LivroBean]" ??? Acredito que esse trecho não é a causa do problema

A mensagem inteira é muito grande, então fiz o upload aqui, como sugerido pela plataforma do fórum: https://gist.github.com/arajag/b8ce092543e1692660facff0a7ddeb61#file-gistfile1-txt

Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.validator.internal.engine.ConfigurationImpl

Me parece incompatibilidade do Hibernate com o JDK que você está usando, tenta usar o projeto com JDK 8 ou JDK 11.

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