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

Target Unreachable, identifier 'adminLivrosBean' resolved to null

Já revisei as aulas e o meu código várias vezes e não estou conseguindo encontrar o que está causando o erro:

WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-11) /livros/form.xhtml @11,59 value="#{adminLivrosBean.livro.titulo}": Target Unreachable, identifier 'adminLivrosBean' resolved to null: javax.el.PropertyNotFoundException: /livros/form.xhtml @11,59 value="#{adminLivrosBean.livro.titulo}": Target Unreachable, identifier 'adminLivrosBean' resolved to null

package br.com.casadocodigo.loja.beans;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

import br.com.casadocodigo.loja.models.Livro;

// CDI
@Named
@RequestScoped
public class AdminLivrosBean {

    private Livro livro = new Livro();

    public void salvar() {
        System.out.println("Livro cadastrado: " + livro.toString());
    }

    public Livro getLivro() {
        return livro;
    }

    public void setLivro(Livro livro) {
        this.livro = livro;
    }        
}
<!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://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core">


    <h:form>
        <div>
            <h:outputLabel value="Título" />
            <h:inputText value="#{adminLivrosBean.livro.titulo}" />
        </div>
        <div>
            <h:outputLabel value="Descrição" />
            <h:inputTextarea rows="4" cols="20" value="#{adminLivrosBean.livro.descricao}" />
        </div>
        <div>
            <h:outputLabel value="Número de Páginas" />
            <h:inputText value="#{adminLivrosBean.livro.numeroPaginas}" /> 
        </div>
        <div>
            <h:outputLabel value="Preço"/>
            <h:inputText value="#{adminLivrosBean.livro.preco}" />
        </div>
        <h:commandButton value="Cadastrar" action="#{adminLivrosBeans.salvar}" />
    </h:form>


</html>

Meus arquivos beans.xml e faces-conf.xml em src/main/webapp/WEB-INF

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bean-discovery-mode="all" version="1.1" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.2" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"/>
2 respostas

Alguns fragmentos do log do console, já que o log completo não é possível devido ao tamanho

09:49:24,173 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 77) WFLYUT0022: Unregistered web context: /casadocodigo
09:49:24,178 INFO  [org.jboss.weld.deployer] (MSC service thread 1-2) WFLYWELD0010: Stopping weld service for deployment casadocodigo.war
09:49:24,182 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 79) WFLYJPA0011: Stopping Persistence Unit (phase 2 of 2) Service 'casadocodigo.war#casadocodigo-dev'
09:49:24,185 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 79) WFLYJPA0011: Stopping Persistence Unit (phase 1 of 2) Service 'casadocodigo.war#casadocodigo-dev'
09:49:24,187 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-5) WFLYSRV0028: Stopped deployment casadocodigo.war (runtime-name: casadocodigo.war) in 15ms
09:49:24,205 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) WFLYSRV0009: Undeployed "casadocodigo.war" (runtime-name: "casadocodigo.war")
09:49:29,223 INFO  [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) WFLYDS0004: Found casadocodigo.war in deployment directory. To trigger deployment create a file called casadocodigo.war.dodeploy
09:49:29,233 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-8) WFLYSRV0027: Starting deployment of "casadocodigo.war" (runtime-name: "casadocodigo.war")
09:49:29,279 INFO  [org.jboss.as.jpa] (MSC service thread 1-5) WFLYJPA0002: Read persistence.xml for casadocodigo-dev
09:49:29,304 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 79) WFLYJPA0010: Starting Persistence Unit (phase 1 of 2) Service 'casadocodigo.war#casadocodigo-dev'
09:49:29,304 INFO  [org.hibernate.jpa.internal.util.LogHelper] (ServerService Thread Pool -- 79) HHH000204: Processing PersistenceUnitInfo [
    name: casadocodigo-dev
    ...]
09:49:29,306 INFO  [org.hibernate.orm.deprecation] (ServerService Thread Pool -- 79) HHH90000001: Found usage of deprecated setting for specifying Scanner [hibernate.ejb.resource_scanner]; use [hibernate.archive.scanner] instead
09:49:29,311 INFO  [org.jboss.weld.deployer] (MSC service thread 1-3) WFLYWELD0003: Processing weld deployment casadocodigo.war
09:49:29,333 INFO  [org.jboss.weld.deployer] (MSC service thread 1-3) WFLYWELD0006: Starting Services for CDI deployment: casadocodigo.war
09:49:29,335 INFO  [org.jboss.weld.deployer] (MSC service thread 1-1) WFLYWELD0009: Starting weld service for deployment casadocodigo.war
09:49:29,338 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 79) WFLYJPA0010: Starting Persistence Unit (phase 2 of 2) Service 'casadocodigo.war#casadocodigo-dev'
09:49:29,339 INFO  [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 79) HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
09:49:29,342 INFO  [org.hibernate.envers.boot.internal.EnversServiceImpl] (ServerService Thread Pool -- 79) Envers integration enabled? : true
09:49:29,351 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (ServerService Thread Pool -- 79) HHH000228: Running hbm2ddl schema update
09:49:29,448 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 79) Inicializando Mojarra 2.2.12-jbossorg-2 20150729-1131 para o contexto '/casadocodigo'
09:49:29,500 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 79) WFLYUT0021: Registered web context: /casadocodigo
09:49:29,521 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) WFLYSRV0010: Deployed "casadocodigo.war" (runtime-name : "casadocodigo.war")
09:49:57,027 WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-11) /livros/form.xhtml @11,59 value="#{adminLivrosBean.livro.titulo}": Target Unreachable, identifier 'adminLivrosBean' resolved to null: javax.el.PropertyNotFoundException: /livros/form.xhtml @11,59 value="#{adminLivrosBean.livro.titulo}": Target Unreachable, identifier 'adminLivrosBean' resolved to null
solução!

Olá, Cleyton! Tudo bem?

Pelo código que você compartilhou, parece que o problema está na sua página XHTML. Na tag <h:commandButton>, você está chamando o método salvar do bean adminLivrosBeans, mas o nome correto do bean é adminLivrosBean (sem o "s" no final).

Portanto, para corrigir o erro, você precisa alterar a linha <h:commandButton value="Cadastrar" action="#{adminLivrosBeans.salvar}" /> para <h:commandButton value="Cadastrar" action="#{adminLivrosBean.salvar}" />.

Após fazer essa alteração, o método salvar do bean AdminLivrosBean será chamado corretamente quando o botão "Cadastrar" for clicado.

Espero ter ajudado e bons estudos!