Error occurs when performing resource injection into the bean

Asked

Viewed 1,859 times

1

Bean:

package br.com.drogaria.bean;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.ListDataModel;
import br.com.drogaria.dao.FabricanteDAO;
import br.com.drogaria.domain.Fabricante;

@ManagedBean(name = "MBfabricantes")
@ViewScoped
public class FabricanteBean {
    private ListDataModel<Fabricante> fResultSearch;
    public ListDataModel<Fabricante> getfResultSearch() {
        return fResultSearch;
    }
    public void setfResultSearch(ListDataModel<Fabricante> fResultSearch) {
        this.fResultSearch = fResultSearch;
    }

    @PostConstruct
    public void catalogo() {
        FabricanteDAO fdao = new FabricanteDAO();

        try {
            ArrayList<Fabricante> list = fdao.listarTudo();
            fResultSearch = new ListDataModel<Fabricante>(list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Page:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:p="http://primefaces.org/ui"
    template="/template/modeloSistema.xhtml">

    <ui:define name="menu">
        <ui:include src="/includes/menuPrincipal.xhtml" />
    </ui:define>

    <ui:define name="conteudo">
        <p:dataTable emptyMessage="Nada encontrado :("
            value="#{MBfabricantes.fResultSearch}" var="item">

            <p:column headerText="Código">
                <h:outputText value="#{item.codigo}" />
            </p:column>

            <p:column headerText="Descrição">
                <h:outputText value="#{item.descricao}" />
            </p:column>

        </p:dataTable>
    </ui:define>
</ui:composition>

Exception:

javax.servlet.Servletexception: An error occurred while injecting the resource into the Mbmanufacturers managed bean javax.faces.webapp.FacesServlet.service(Facesservlet.java:659) org.apache.Tomcat.websocket.server.WsFilter.doFilter(Wsfilter.java:52)

  • 1

    Post the full stacktrace, there must have been some error in the PostConstruct. And change the SQLException by a more generic exception, just to check the stacktrace.

2 answers

1

An additional to your own answer (This should be a comment, but strangely in some questions I am unable to comment (I am new user)).

You do Drivermanager.registerDriver(Driver driver) may be redundant. What was occurring is that your application’s Drivermanager was not being notified to register the driver. See the excerpt from Static initialization of the class with.mysql.jdbc.Driver:

    //
    // Register ourselves with the DriverManager
    //
    static {
        try {
            java.sql.DriverManager.registerDriver(new Driver());
        } catch (SQLException E) {
            throw new RuntimeException("Can't register driver!");
        }
    }

Not that this is important, but it’s the way JDBC Drivers work (they only need to be triggered once to be automatically registered). To avoid a strong coupling directly from your class with the driver, choose to do

Class.forName("com.mysql.jdbc.Driver");

Usually on an application server, this code snippet is not required.

1

The resolution of that was:

In the manufacture of connections, specifically in the method, put the following command (although my version of Java is 7):

DriverManager.registerDriver(new com.mysql.jdbc.Driver());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.