Livro Java EE( Erro ao tentar salvar)

java-ee

#1

Olá, estou tentando salvar no banco, mas obtenho esse erro:
Context Path:/casadocodigo
Servlet Path:/produtos/form.xhtml
Path Info:null
Query String:null
Stack Trace
javax.servlet.ServletException: /produtos/form.xhtml @13,59 value="#{adminBooksBean.product.title}": Target Unreachable, identifier ‘adminBooksBean’ resolved to null
javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61)
io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)
io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:63)
io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)
io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:261)
io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:247)
io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:76)
io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:166)
io.undertow.server.Connectors.executeRootHandler(Connectors.java:197)
io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:759)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)


#2

A classe AdminBooksBean está com as anotations corretas?


#3

Olá, Gracyane! Todas as anotations estão corretas dentro de AdminBooksBean. Consegui salvar no banco, quando usei a anotation @ManagedBean. Porém, no projeto “casadocodigo” o autor usa somente @Model. O autor diz que: “Ela é uma annotation do CDI que indica que os objetos da classe por ela anotada sempre serão criados no escopo de request , e ficarão disponíveis para serem acessados pela ExpressionLanguage.” Num primerio momento até, antes de fazer a configuração do JPA, isso funcionou, podendo exibir as informações passadas pelo form.xhtml no console. Porém, depois deste ponto somente com a anotation @ManagedBean para funcionar.

– Classe AdminBooksBean–
package br.com.casadocodigo.loja.managedbeans;

import javax.annotation.ManagedBean;
import javax.enterprise.inject.Model;
import javax.inject.Inject;
import javax.transaction.Transactional;

import br.com.casadocodigo.loja.daos.BookDAO;
import br.com.casadocodigo.loja.models.Book;
@ManagedBean
@Model
public class AdminBooksBean {

private Book product=new Book();

@Inject
private BookDAO bookDao;
@Transactional
public void save() {
	bookDao.save(product);
	//System.out.println("Precisamos	salvar	o	livro!!"+getProduct());
}

public Book getProduct() {
	return product;
}

public void setProduct(Book product) {
	this.product = product;
}

}


#4

Ainda, prossegindo no curso Java EE. Consegui persistir, mas me deparei novamente com mesmo erro( ao adicionar novas linhas na classe AdminBooksBean) que me perseguia. Isso aconteceu mesmo usando a anotation @ManagedBean que teria resolvido o problema para mim no primeiro caso. OBS:“Não consegui listar autores e nem a tabela author foi criada no banco”.
– Classe AdminBooksBean modificada--

package br.com.casadocodigo.loja.managedbeans;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.ManagedBean;
import javax.annotation.PostConstruct;
import javax.enterprise.inject.Model;
import javax.inject.Inject;
import javax.transaction.Transactional;

import br.com.casadocodigo.loja.daos.AuthorDAO;
import br.com.casadocodigo.loja.daos.BookDAO;
import br.com.casadocodigo.loja.models.Author;
import br.com.casadocodigo.loja.models.Book;

@ManagedBean
@Model
public class AdminBooksBean {

private Book product = new Book();
private List<Integer> selectedAuthorsIds = new ArrayList<>();
private	List<Author> authors=new ArrayList<Author>();

@Inject
private BookDAO bookDao;
@Inject 
private AuthorDAO authorDAO;


@PostConstruct
public void loadObjects(){								
	this.authors =authorDAO.list();
}



@Transactional
public void save() {
	populateBookAuthor();								
	bookDao.save(product);
	// System.out.println("Precisamos salvar o livro!!"+getProduct());
}


private void populateBookAuthor() {
	selectedAuthorsIds.stream().map( (id) -> {
		return new Author(id);
	}).forEach(product :: add);
}

public Book getProduct() {
	return product;
}

public void setProduct(Book product) {
	this.product = product;
}


public List<Author> getAuthors() {
	return authors;
}


public void setAuthors(List<Author> authors) {
	this.authors = authors;
}


public List<Integer> getSelectedAuthorsIds() {
	return selectedAuthorsIds;
}


public void setSelectedAuthorsIds(List<Integer> selectedAuthorsIds) {
	this.selectedAuthorsIds = selectedAuthorsIds;
}

}

– Erro–
Error processing request
Context Path:
/casadocodigo

Servlet Path:
/produtos/form.xhtml

Path Info:
null

Query String:
null

Stack Trace
javax.servlet.ServletException: /produtos/form.xhtml @13,59 value="#{adminBooksBean.product.title}": Target Unreachable, identifier ‘adminBooksBean’ resolved to null
javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61)
io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)
io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:63)
io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)
io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:261)
io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:247)
io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:76)
io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:166)
io.undertow.server.Connectors.executeRootHandler(Connectors.java:197)
io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:759)


#5

Problema resolvido. O forge, ao inserir as dependências no arquivo pom.xml, trocou as versões ao qual o autor está trabalhando. Portanto foi somente copiar o conteúdo do arquivo pom.xml do Livro Java EE e tudo funcionou perfeitamente como está no livro.


#6

Olá Israel, que bom que conseguiu resolver.


#7

Obrigado, Gracyane!!!