Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Erro execício 09 aula Completando o sistema e lidando com escopos do JSF2

No ultimo exercício pede para mostrar uma lista com todos os livros cadastrados, porém aparece a seguinte tela no browser:

HTTP Status 500 - javax/servlet/jsp/jstl/sql/Result

type Exception report

message javax/servlet/jsp/jstl/sql/Result

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: javax/servlet/jsp/jstl/sql/Result
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
root cause

java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/sql/Result
    javax.faces.component.UIData.getDataModel(UIData.java:1806)
    javax.faces.component.UIData.setRowIndexWithoutRowStatePreserved(UIData.java:483)
    javax.faces.component.UIData.setRowIndex(UIData.java:472)
    com.sun.faces.renderkit.html_basic.TableRenderer.encodeBegin(TableRenderer.java:82)
    javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:820)
    javax.faces.component.UIData.encodeBegin(UIData.java:1117)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1777)
    javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:437)
    com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:124)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.42 logs.

Apache Tomcat/7.0.42

Minha classe LivroBean:

package br.com.caelum.livraria.bean;

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 {

    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<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 gravaAutor(){
        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();
    }

}

Meu arquivo 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>

                <h:outputLabel value="Selecione o 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.gravaAutor}"/>

                <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:dataTable id="tabelaLivros" value="#{livroBean.livro}" var="livro">

              <h:column>
                <f:facet name="header">Título</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:column>
                <f:facet name="header">Data</f:facet>
                <h:outputText value="#{livro.dataLancamento}" />
              </h:column>
            </h:dataTable>
            </h:form>


</h:body>

</html>
1 resposta
solução!

Opa Guilherme, tudo bem?

A resolução aparentemente é simples. No seu data table você está chamando livroBean.livro mas você precisa chamar a lista de livros, ficando livroBean.livros no plural.

Você pode testar e ver se funciona?

Abs!

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