Inject the Dao into the Converter

2

I'm having a java.lang.NullPointerException error in a converter at the moment it will access the bank, since the dependency is not being injected by the CDI, there are arguments that the Converter should not access the Bank any more my case I am not able to make the view with a dialog of the Primefaces to work, without the Dialogo it works, with it not so why the Converter changed, more how to make it work?

Project with JSF 2.2 + CDI + JPA, in Tomcat 8 Java 8 I have already seen Convert with the injection of Dao only that I am not succeeding the error is     Caused by: java.lang.NullPointerException     at .com.converter.StateConverter.getAsObject (StatusConverter.java:29) I do not know if in projects I have seen this type of converter have ... let's say the data (the EntityManager in session) will see that the instance is being created plus the EntityManager is the problem?

Follow the MB

@Named(value = "municipioC")
@ViewScoped
public class MunicipioController extends AbstractMB {

private static final long serialVersionUID = 1L;

private int totalRegistros;
private List<Municipio> elementos;
private List<Estado> estados;
private Municipio registroSelecionado;
private Municipio municipio;

@Inject
private MunicipioService municipioService;

@Inject
private EstadoService estadoService;

public MunicipioController() {
}

Follow the Converter

FacesConverter(forClass = Estado.class)
public class EstadoConverter implements Converter {

@Inject
EstadoDao dao ;

@Override
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) {

    System.out.println("EstadoConverter - getAsObject - valor: "+value);        

     Estado estado = null;

        if (StringUtils.isNotEmpty(value)){
            Integer Id = Integer.parseInt(value);
            // Na linha Abaixo ocorre o erro 
            estado = this.dao.findByID(Id);
        }

      return estado;        

}

and the view

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
template="/WEB-INF/templates/Layout.xhtml">

<ui:define name="content">
    <h1 class="aw-page-title">Municípios</h1>

    <h:form id="frm">

        <p:toolbar>
            <p:toolbarGroup>
                <p:commandButton id="btnAdd" title="Novo registro"
                    action="#{municipioC.preparaInclusao}"
                    oncomplete="PF('itemDialog').show()" process="@this"
                    update=":itemForm:itemPanel" icon="fa fa-fw fa-plus" />

                <p:commandButton id="btnEdi" title="Edita registro"
                    action="#{municipioC.preparaAlteracao}"
                    oncomplete="PF('itemDialog').show()" process="@this"
                    update=":itemForm:itemPanel" icon="fa fa-fw fa-edit" />

            </p:toolbarGroup>
        </p:toolbar>

        <p:messages autoUpdate="false" />
        <p:dataTable id="itensTable" widgetVar="dataTable" var="item"
            loadingMessage="Carregando..." emptyMessage="Nenhum registro."
            reflow="true" value="#{municipioC.elementos}" selectionMode="single"
            selection="#{municipioC.registroSelecionado}"
            rowKey="#{item.id}" style="margin-top:10px">

            <p:ajax event="rowSelect" listener="#{municipioC.selectRegistro}" />

            <p:column headerText="Id" style="width:50px">
                <h:outputText value="#{item.id}" />
            </p:column>

            <p:column headerText="Municipio">
                <h:outputText value="#{item.dsMunicipio}" />
            </p:column>

            <p:column headerText="Uf">
                <h:outputText value="#{item.estado.siglaEstado}" />
            </p:column>

        </p:dataTable>
    </h:form>

    <p:dialog widgetVar="itemDialog" id="iditemDialog" header="Gerencia municipios"
        responsive="true" resizable="false" showEffect="explode"
        hideEffect="bounce" style="min-width: 400px" modal="true" appendTo="@(body)">

        <h:form id="itemForm">

            <h:panelGroup id="itemPanel" layout="block" styleClass="ui-fluid"
                style="margin-top:10px;">
                <p:messages id="msgDialog" autoUpdate="false" />

                <p:panelGrid columns="2" layout="grid"
                    styleClass="panelgrid-noborder"
                    columnClasses="ui-grid-col-4, ui-grid-col-8">

                    <p:outputLabel for="nome" value="Municipio:" />
                    <p:inputText id="nome" value="#{municipioC.municipio.dsMunicipio}"
                        placeholder="Municipio" size="50" />
                    <p:outputLabel for="ibge" value="Codigo IBGE:" />
                    <p:inputText id="ibge" value="#{municipioC.municipio.codIbge}"
                        placeholder="Codigo IBGE" size="10" />

                    <p:outputLabel for="estado" value="Estado" />
                    <p:selectOneMenu id="estado"
                        value="#{municipioC.municipio.estado}" required="true">
                        <f:selectItem itemLabel="Selecione" />
                        <f:selectItems value="#{municipioC.estados}" var="itemE"
                            itemValue="#{itemE}" itemLabel="#{itemE.dsEstado}" />
                    </p:selectOneMenu>

                </p:panelGrid>

            </h:panelGroup>

            <p:commandButton icon="fa fa-fw fa-save" title="Salvar"
                        action="#{municipioC.salvar}" process="itemPanel"
                        update="itemPanel"
                        oncomplete="closeDialogIfSucess(xhr, status, args, PF('itemDialog'), 'itemDialog')" />

        </h:form>
    </p:dialog>

</ui:define>
    
asked by anonymous 08.07.2016 / 01:49

1 answer

0

Unfortunately @Inject does not work with @FacesConverter annotation.

There are other ways to resolve this problem. One of them is to replace the annotation:

@FacesConverter(forClass = Estado.class)

by

@Named

and use

<p:selectOneMenu converter="#{estadoConverter}" />

Another way would be to use the OmniFaces library, which since 1.6 has support for @Inject in @FacesConverter .

For more details, see this full answer from @BalusC.

    
11.07.2016 / 19:04