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

Aula 08 - exercicio 05 - Falha ao logar com vraptor

Olá estou com erro ao logar com sucesso no meu formulário de login.

Conferindo no debug, parece que o erro ocorre ao passar pelas seguintes linhas do LoginController:

        usuarioLogado.setUsuario(usuario);
        result.redirectTo(ProdutoController.class).lista();

Segue a mensagem de erro com maiores detalhes e o fonte:

HTTP Status 500 - net.vidageek.mirror.exception.ReflectionProviderException: Could not invoke method autentica

type Exception report

message net.vidageek.mirror.exception.ReflectionProviderException: Could not invoke method autentica

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

exception

javax.servlet.ServletException: net.vidageek.mirror.exception.ReflectionProviderException: Could not invoke method autentica
    br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:120)
root cause

net.vidageek.mirror.exception.ReflectionProviderException: Could not invoke method autentica
    net.vidageek.mirror.provider.java.PureJavaMethodReflectionProvider.invoke(PureJavaMethodReflectionProvider.java:45)
    net.vidageek.mirror.invoke.MethodHandlerByMethod.withArgs(MethodHandlerByMethod.java:54)
    br.com.caelum.vraptor.observer.ExecuteMethod.execute(ExecuteMethod.java:87)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:93)
    org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:266)
    org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:253)
    org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:232)
    org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:169)
    org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:165)
    org.jboss.weld.event.ObserverNotifier.notifyObservers(ObserverNotifier.java:119)
    org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:112)
    org.jboss.weld.event.EventImpl.fire(EventImpl.java:83)
    br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:78)
    br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor.intercept(ExceptionHandlerInterceptor.java:75)
    br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor$Proxy$_$$_WeldClientProxy.intercept(Unknown Source)
    br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:58)
    br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:83)
    br.com.caelum.vraptor.interceptor.FlashInterceptor.intercept(FlashInterceptor.java:98)
    br.com.caelum.vraptor.interceptor.FlashInterceptor$Proxy$_$$_WeldClientProxy.intercept(Unknown Source)
    br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:58)
    br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:83)
    br.com.caelum.vraptor.core.DefaultInterceptorStack.start(DefaultInterceptorStack.java:93)
    br.com.caelum.vraptor.core.DefaultInterceptorStack$Proxy$_$$_WeldClientProxy.start(Unknown Source)
    br.com.caelum.vraptor.observer.RequestHandlerObserver.handle(RequestHandlerObserver.java:86)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:93)
    org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:266)
    org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:253)
    org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:232)
    org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:169)
    org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:165)
    org.jboss.weld.event.ObserverNotifier.notifyObservers(ObserverNotifier.java:119)
    org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:112)
    org.jboss.weld.event.EventImpl.fire(EventImpl.java:83)
    br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:116)
root cause

java.lang.NullPointerException
    br.com.caelum.vraptor.controller.LoginController.autentica(LoginController.java:51)
    br.com.caelum.vraptor.controller.LoginController$Proxy$_$$_WeldClientProxy.autentica(Unknown Source)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    net.vidageek.mirror.provider.java.PureJavaMethodReflectionProvider.invoke(PureJavaMethodReflectionProvider.java:38)
    net.vidageek.mirror.invoke.MethodHandlerByMethod.withArgs(MethodHandlerByMethod.java:54)
    br.com.caelum.vraptor.observer.ExecuteMethod.execute(ExecuteMethod.java:87)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:93)
    org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:266)
    org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:253)
    org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:232)
    org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:169)
    org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:165)
    org.jboss.weld.event.ObserverNotifier.notifyObservers(ObserverNotifier.java:119)
    org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:112)
    org.jboss.weld.event.EventImpl.fire(EventImpl.java:83)
    br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:78)
    br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor.intercept(ExceptionHandlerInterceptor.java:75)
    br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor$Proxy$_$$_WeldClientProxy.intercept(Unknown Source)
    br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:58)
    br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:83)
    br.com.caelum.vraptor.interceptor.FlashInterceptor.intercept(FlashInterceptor.java:98)
    br.com.caelum.vraptor.interceptor.FlashInterceptor$Proxy$_$$_WeldClientProxy.intercept(Unknown Source)
    br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:58)
    br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:83)
    br.com.caelum.vraptor.core.DefaultInterceptorStack.start(DefaultInterceptorStack.java:93)
    br.com.caelum.vraptor.core.DefaultInterceptorStack$Proxy$_$$_WeldClientProxy.start(Unknown Source)
    br.com.caelum.vraptor.observer.RequestHandlerObserver.handle(RequestHandlerObserver.java:86)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:93)
    org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:266)
    org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:253)
    org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:232)
    org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:169)
    org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:165)
    org.jboss.weld.event.ObserverNotifier.notifyObservers(ObserverNotifier.java:119)
    org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:112)
    org.jboss.weld.event.EventImpl.fire(EventImpl.java:83)
    br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:116)

Minha LoginController:

package br.com.caelum.vraptor.controller;

@Controller
public class LoginController {

    private final Result result;
    private final Validator validator;
    private UsuarioDao usuarioDao;
    private UsuarioLogado usuarioLogado;

    @Inject
    public LoginController(Result result, Validator validator, UsuarioDao usuarioDao, UsuarioLogado usuarioLogado) {
        this.result = result;
        this.validator = validator;
        this.usuarioDao = usuarioDao;
        usuarioLogado = usuarioLogado;

    }

    @Deprecated
    LoginController() {
        this(null, null, null,null);
    }


    @Get
    public void formulario(){

    }

    @Post
    public void autentica(Usuario usuario){
        if (!usuarioDao.existe(usuario)) {
            validator.add(new I18nMessage("login", "login.invalido"));
            validator.onErrorUsePageOf(this).formulario();
        }
        usuarioLogado.setUsuario(usuario);
        result.redirectTo(ProdutoController.class).lista();

    }
}

Meu login/formulario.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<link rel="stylesheet" type="text/css" href="../bootstrap/css/bootstrap.css">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Faça seu login</title>
</head>
<body>
<div class="container">
 <form class="form-signin" action="<c:url value='/login/autentica'/>" method="post">
  <h2 class="form-signin-heading">Faça login para acessar o VRaptor-Produtos</h2>
  <input type="text" class="form-control" name="usuario.nome" placeholder="Nome"/>
  <input type="password" class="form-control" name="usuario.senha" placeholder="Senha"/>
  <button class="btn btn-lg btn-primary btn-block" type="submit">Login</button>
 </form>
<c:if test="${not empty errors}">
    <div class="alert alert-danger">
        <c:forEach items="${errors}" var="error">
            ${error.category} : ${error.message}<br />
        </c:forEach>
    </div>
</c:if>
</div>
</body>
</html>

Minha UsuarioLogado:

package br.com.caelum.vraptor.model;

import java.io.Serializable;

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

public class UsuarioLogado implements Serializable{

    @SessionScoped
    @Named
    private Usuario usuario;

    public Usuario getUsuario() {
        return usuario;
    }

    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }


}

Meu ProdutoController:

package br.com.caelum.vraptor.controller;


@Controller
public class ProdutoController {

    private final Result result;
    private final ProdutoDao dao;
    private final Validator validator;

    @Inject
    public ProdutoController(Result result, ProdutoDao dao, Validator validator) {
        this.result = result;
        this.dao = dao;
        this.validator = validator;
    }

    @Deprecated
    ProdutoController() {
        this(null,null,null); //para uso do CDI
    }



    @Path("/")
    public void inicio(){

    }

    @Get
    public void lista(){
        result.include("listagem", dao.lista());

    }

    @Get
    public void listaXML(){
        result.use(Results.xml()).from(dao.lista()).serialize();    
    }

    @Get
    public void sobre(){

    }

    @Get
    public void formulario(){

    }

    @Post
    public void adiciona(@Valid Produto produto){

        /*
        validator.check(produto.getQuantidade() > 0, 
                new I18nMessage("erro","produto.quantidade.negativa"));


        */
        validator.onErrorUsePageOf(this).formulario();
        dao.adiciona(produto);
        result.include("mensagem", "Produto cadastrado com sucesso");
        result.redirectTo(this).lista();

    }

    @Delete
    public void remove (Produto produto){
        dao.remove(produto);
        result.redirectTo(this).lista();
    }

}

Meu produto/lista.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
    <link rel="stylesheet" type="text/css" href="../bootstrap/css/bootstrap.css">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
    </head>
    <body>
        <div class="container">
        <h1>Lista de produtos do ${usuarioLogado.usuario.nome}</h1>
        <table class="table table-stripped table-over table-bordered">
            <thead>
                <tr>
                    <th>Nome</th>
                    <th>Valor</th>
                    <th>Quantidade</th>
                </tr>
            </thead>
            <tbody>
                <c:forEach items="${listagem}" var="produto">
                    <tr>
                        <td>${produto.nome}</td>
                        <td>${produto.valor}</td>
                        <td>${produto.quantidade}</td>
                        <td>
                            <form action="/produto/remove">
                                <input type="hidden" name="produto.id" value="${produto.id}">
                                <button type="submit" name="_method" value="DELETE">Remover</button>
                            </form>
                        </td>
                        <td><a href="<c:url value='/produto/remove?produto.id=${produto.id}' />">Remover</a>
                        </td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>
        </div>
        <c:if test="${not empty mensagem}">
            <div class="alert alert-success">${mensagem}</div>
        </c:if>
        <a href="<c:url value='/produto/formulario'/>"> adicione mais produtos</a></br>
        <a href="<c:url value='/produto/listaXML'  />"> veja listaXML </a>
</body>
</html>

Desde já agradeço toda ajuda.

2 respostas
solução!

O erro ta aqui =>

usuarioLogado = usuarioLogado;
this.usuarioLogado = usuarioLogado;

Valeu Alberto, era isso mesmo.

Muito obrigado.