Solucionado (ver solução)
Solucionado
(ver solução)
5
respostas

Entendendo MVC e Integrando o banco de dados com JPA2

Bom dia amigos!!

Estou adaptando o código da aula para um pequeno projeto particular... Consegui popular o banco mas não consigo visualizar os dados. Aparece apenas os Legends

<fieldset>
                <legend>Dados do Projeto</legend>
                <h:panelGrid columns="2">

Alguém pode me ajudar? Gostaria de enviar o print da tela ou mesmo o projeto como todo por email. Já tentei, mas não consegui!

5 respostas

Erro no console

ago 18, 2016 8:58:55 AM com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException
GRAVE: Error Rendering View[/projeto.xhtml]
javax.el.PropertyNotFoundException: /projeto.xhtml @15,69 value="#{projetoBean.projeto.sigla}": Property 'projeto' not found on type br.com.tce.atividade.bean.ProjetoBean
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:111)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UIOutput.getValue(UIOutput.java:169)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
    at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
    at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:437)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:124)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

ago 18, 2016 8:58:55 AM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [Faces Servlet] in context with path [/controle-atividade] threw exception [/projeto.xhtml @15,69 value="#{projetoBean.projeto.sigla}": Property 'projeto' not found on type br.com.tce.atividade.bean.ProjetoBean] with root cause
javax.el.PropertyNotFoundException: /projeto.xhtml @15,69 value="#{projetoBean.projeto.sigla}": Property 'projeto' not found on type br.com.tce.atividade.bean.ProjetoBean
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:111)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UIOutput.getValue(UIOutput.java:169)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
    at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
    at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:437)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:124)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

ProjetoBean.java

package br.com.tce.atividade.bean;

import java.util.List;

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

import br.com.tce.atividade.dao.DAO;
import br.com.tce.atividade.modelo.Projeto;
import br.com.tce.atividade.modelo.Usuario;

@ManagedBean
@ViewScoped
public class ProjetoBean {

    private Projeto projeto = new Projeto();
    private Integer autorId;

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

    public Integer getAutorId() {
        return autorId;
    }

    public Projeto getLivro() {
        return projeto;
    }

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

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

    public List<Usuario> getAutoresDoLivro() {
        return this.projeto.getUsuarios();
    }

    public void adicionarAutor() {

        Usuario usuario = new DAO<Usuario>(Usuario.class).buscaPorId(this.autorId);
        System.out.println("Usando autor " + usuario.getNome());
        this.projeto.adicionaUsuario(usuario);
    }

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

        if (projeto.getUsuarios().isEmpty()) {
            throw new RuntimeException("Livro deve ter pelo menos um Usuario.");
        }

        new DAO<Projeto>(Projeto.class).adiciona(this.projeto);
    }

}

Projeto.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>Controle de Atividade</h1>
        <h:form>
            <fieldset>
                <legend>Dados do Projeto</legend>
                <h:panelGrid columns="2">

                    <h:outputLabel value="Sigla:" for="sigla" />
                    <h:inputText id="sigla" value="#{projetoBean.projeto.sigla}" />

                    <h:outputLabel value="Descrição:" for="descricao" />
                    <h:inputText id="descricao" value="#{projetoBean.projeto.descricao}" />

                    <h:outputLabel value="Servidor Produção:" for="servidorProducao" />
                    <h:inputText id="servidorProducao"
                        value="#{projetoBean.projeto.servidorProducao}" />

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

                </h:panelGrid>
            </fieldset>

            <fieldset>
                <legend>Equipe de Trabalho</legend>
                <h:panelGrid columns="3">

                    <h:outputLabel value="Autor:" />
                    <h:selectOneMenu value="#{projetoBean.usuarioId}">
                        <f:selectItems value="#{projetoBean.usuarios}" itemValue="#{autor.id}"
                            itemLabel="#{autor.nome}" var="autor" />
                    </h:selectOneMenu>
                    <h:commandButton action="#{projetoBean.adicionarUsuario}"
                        value="Adiciona Usuario" />

                    <h:dataTable value="#{projetoBean.usuariosDoProjeto}" var="usuario">
                        <h:column>
                            <h:outputText value="#{usuario.nome}" />
                        </h:column>
                    </h:dataTable>
                </h:panelGrid>
            </fieldset>

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

        <h:dataTable value="#{projetoBean.projetos}" var="projeto">
            <h:column>
                <f:facet name="header">Sigla</f:facet>
                <h:outputText value="#{projeto.sigla}" />
            </h:column>
            <h:column>
                <f:facet name="header">Descrição</f:facet>
                <h:outputText value="#{projeto.descricao}" />
            </h:column>
            <h:column>
                <f:facet name="header">Data</f:facet>
                <h:outputText value="#{projeto.dataLancamento}" />
            </h:column>
        </h:dataTable>
    </h:body>

</html>

No seu ManagedBean não tem o método getProjeto, é bem isso que a exception ta falando pelo menos :). Tenta adicionar lá, veremos o que acontece :).

solução!

Obrigado Alberto,

Deu certo!! Me ajudou a descobrir outros pequenos erros.