[RESOLVIDO] VRAPTOR E FJ28 - Problemas com a criação do ProdutosController

2 respostas
C

Estou lendo a apostila FJ28, e tive alguns problemas com a criação do ProdutosController..
Ele não apresenta nenhum erro, apenas não acessa a página...

Segue o código abaixo:

package br.com.caelum.goodbuy.modelo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Produto
{
	@Id
	@GeneratedValue
	private Long id;

	private String nome;

	private String descricao;

	private Double preco;

	public Long getId()
	{
		return id;
	}

	public void setId(Long id)
	{
		this.id = id;
	}

	public String getNome()
	{
		return nome;
	}

	public void setNome(String nome)
	{
		this.nome = nome;
	}

	public String getDescricao()
	{
		return descricao;
	}

	public void setDescricao(String descricao)
	{
		this.descricao = descricao;
	}

	public Double getPreco()
	{
		return preco;
	}

	public void setPreco(Double preco)
	{
		this.preco = preco;
	}

}
package br.com.caelum.goodbuy.dao;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import br.com.caelum.goodbuy.infra.CriadorDeSession;
import br.com.caelum.goodbuy.modelo.Produto;
import br.com.caelum.vraptor.ioc.Component;

@Component
public class ProdutoDao
{
	private final Session session;

	public ProdutoDao()
	{
		this.session = CriadorDeSession.getSession();
	}

	public void atualiza(Produto produto, Session session)
	{

		Transaction tx = session.beginTransaction();
		session.update(produto);
		tx.commit();
	}

	public void exclui(Produto produto, Session session)
	{

		Transaction tx = session.beginTransaction();
		session.delete(produto);
		tx.commit();
	}

	public void salva(Produto produto)
	{

		Transaction tx = session.beginTransaction();
		session.save(produto);
		tx.commit();

	}

	public List<Produto> listaTudo()
	{
		return this.session.createCriteria(Produto.class).list();
	}

}
package br.com.caelum.goodbuy.controller;

import java.util.List;
import javax.annotation.Resource;
import br.com.caelum.goodbuy.dao.ProdutoDao;
import br.com.caelum.goodbuy.modelo.Produto;

@Resource
public class ProdutosController
{

	private final ProdutoDao dao;

	public ProdutosController(ProdutoDao dao)
	{
		this.dao = dao;
	}

	public List<Produto> lista()
	{
		return dao.listaTudo();
	}
}
E o erro:
09:54:57,250 DEBUG [VRaptor             ] VRaptor received a new request
09:54:57,250 DEBUG [DefaultRequestExecution] executing stack  DefaultRequestExecution
09:54:57,265 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor ResourceLookupInterceptor
09:54:57,265 DEBUG [DefaultResourceTranslator] trying to access /produtos/lista
09:54:57,265 DEBUG [VRaptor             ] VRaptor ended the request

2 Respostas

C

O problema no erro anterior era o import da @Resource, mas agora já coloquei o import certo… e o erro agora é o seguinte:

12/03/2012 10:46:21 org.apache.catalina.core.StandardWrapperValve invoke GRAVE: Servlet.service() for servlet default threw exception org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'produtosController' defined in file [C:\Documents and Settings\User\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\goodbuy\WEB-INF\classes\br\com\caelum\goodbuy\controller\ProdutosController.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [br.com.caelum.goodbuy.dao.ProdutoDao]: : Error creating bean with name 'produtoDao' defined in file [C:\Documents and Settings\User\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\goodbuy\WEB-INF\classes\br\com\caelum\goodbuy\dao\ProdutoDao.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [br.com.caelum.goodbuy.dao.ProdutoDao]: Constructor threw exception; nested exception is org.hibernate.HibernateException: JDBC Driver class not found: com.mysql.jdbc.Driver; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'produtoDao' defined in file [C:\Documents and Settings\User\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\goodbuy\WEB-INF\classes\br\com\caelum\goodbuy\dao\ProdutoDao.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [br.com.caelum.goodbuy.dao.ProdutoDao]: Constructor threw exception; nested exception is org.hibernate.HibernateException: JDBC Driver class not found: com.mysql.jdbc.Driver at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:698) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:192) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:984) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:886) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:479) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450) at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:328) at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:43) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:385) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:375) at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1069) at org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:221) at br.com.caelum.vraptor.ioc.spring.VRaptorApplicationContext.getBean(VRaptorApplicationContext.java:253) at br.com.caelum.vraptor.ioc.spring.SpringBasedContainer.instanceFor(SpringBasedContainer.java:59) at br.com.caelum.vraptor.interceptor.InstantiateInterceptor.intercept(InstantiateInterceptor.java:41) at br.com.caelum.vraptor.core.InstantiatedInterceptorHandler.execute(InstantiatedInterceptorHandler.java:47) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:65) at br.com.caelum.vraptor.interceptor.InterceptorListPriorToExecutionExtractor.intercept(InterceptorListPriorToExecutionExtractor.java:46) at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:65) at br.com.caelum.vraptor.interceptor.FlashInterceptor.intercept(FlashInterceptor.java:81) at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:65) at br.com.caelum.vraptor.interceptor.ResourceLookupInterceptor.intercept(ResourceLookupInterceptor.java:67) at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:65) at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:56) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:65) at br.com.caelum.vraptor.core.DefaultRequestExecution.execute(DefaultRequestExecution.java:70) at br.com.caelum.vraptor.VRaptor$1.insideRequest(VRaptor.java:92) at br.com.caelum.vraptor.ioc.spring.SpringProvider.provideForRequest(SpringProvider.java:56) at br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:89) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'produtoDao' defined in file [C:\Documents and Settings\User\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\goodbuy\WEB-INF\classes\br\com\caelum\goodbuy\dao\ProdutoDao.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [br.com.caelum.goodbuy.dao.ProdutoDao]: Constructor threw exception; nested exception is org.hibernate.HibernateException: JDBC Driver class not found: com.mysql.jdbc.Driver at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:946) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:890) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:479) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450) at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:328) at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:43) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:820) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:762) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:680) at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:771) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:691) ... 45 more Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [br.com.caelum.goodbuy.dao.ProdutoDao]: Constructor threw exception; nested exception is org.hibernate.HibernateException: JDBC Driver class not found: com.mysql.jdbc.Driver at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:141) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:72) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:938) ... 57 more Caused by: org.hibernate.HibernateException: JDBC Driver class not found: com.mysql.jdbc.Driver at org.hibernate.connection.DriverManagerConnectionProvider.configure(DriverManagerConnectionProvider.java:89) at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:137) at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:79) at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:425) at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:89) at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2119) at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2115) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1339) at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867) at br.com.caelum.goodbuy.infra.CriadorDeSession.getSession(CriadorDeSession.java:15) at br.com.caelum.goodbuy.dao.ProdutoDao.<init>(ProdutoDao.java:17) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126) ... 59 more Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:192) at org.hibernate.connection.DriverManagerConnectionProvider.configure(DriverManagerConnectionProvider.java:84) ... 74 more

C

Resolvido, faltava o mysql-connector-java-5.1.6-bin no buildpath!

Criado 12 de março de 2012
Ultima resposta 12 de mar. de 2012
Respostas 2
Participantes 1