Load objects from other classes

0

I have selectOneMenu that will serve to list all Generations (data in the database). These data must be listed at the time of registering a Nature object. My selectOneMenu looks like this:

<h:outputLabel value="#{msg['geracao']}" />
<p:selectOneMenu id="geracao" value="#{msg['geracao']}">
<f:selectItem value="" />
<f:selectItems value="#{naturemb.geracoes}"/>
</p:selectOneMenu>
<p:message for="geracao" />

The Nature Controller looks like this:

package br.com.pokemax.controle;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;

import br.com.pokemax.modelo.Geracao;
import br.com.pokemax.modelo.Nature;
import br.com.pokemax.negocio.NatureDAO;
import br.com.pokemax.util.MensagensUtil;

@ViewScoped
@ManagedBean(name = "naturemb")
public class ControleNature implements Serializable {

    private static final long serialVersionUID = 1L;

    private Nature nature;

    @Inject
    private NatureDAO dao;

    private List<Nature> lista;

    private List<Geracao> geracoes;

    @PostConstruct
    public void inicio() {

    }

    public void novo() {
        nature = new Nature();
    }

    public void gravar() {
        try {
            if (nature.getId() == null) {
                dao.insert(nature);
                MensagensUtil.msg("Info", "cadastro.sucesso", new Object[] { MensagensUtil.get("nature") });
                nature = new Nature();
            } else {
                dao.update(nature);
                MensagensUtil.msg("Info", "alterado.sucesso", new Object[] { MensagensUtil.get("nature") });
            }

        } catch (Exception e) {
            e.getMessage();
            return;
        }

    }

    public void pesquisar() {
        try {
            lista = dao.findAll();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void excluir(Nature h) {
        try {
            dao.delete(h);
            MensagensUtil.msg("Info", "removido.sucesso", new Object[] { MensagensUtil.get("nature") });
            pesquisar();
        } catch (Exception e) {
            e.getMessage();
        }
    }

    public void editar(Long id) {
        try {
            setNature(dao.find(id));
        } catch (Exception e) {
            e.getMessage();
        }

    }

    public void cancelar() {
        nature = null;

    }

    public Nature getNature() {
        return nature;
    }

    public List<Geracao> getGeracoes() {
        return geracoes;
    }

    public void setGeracoes(List<Geracao> geracoes) {
        this.geracoes = geracoes;
    }

    public void setNature(Nature nature) {
        this.nature = nature;
    }

    public List<Nature> getLista() {
        return lista;
    }

    public void setLista(List<Nature> lista) {
        this.lista = lista;
    }

}

How do I load all Generations in my selectOneMenu ?

    
asked by anonymous 21.10.2016 / 22:24

2 answers

0

By quickly parsing your code, I noticed that the search method was not processed, it is only implemented. Invoke it inside the start method, because I saw that it has a @PostConstruct or on your screen insert a f: metadata tag and call it, like this:

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

<f:metadata>
    <f:event listener="#{seuBean.pesquisar}" type="preRenderView" />
</f:metadata>
    
23.10.2016 / 00:43
0

On the converter is necessary because in JSF pages the data are Strings, and when you return a list the data are objects and need to be converted into Strings and briefly this is the role of the converter, below is an example of the implementation of a convert and also call it from the view:

// Convert

@Named("fabricanteConverter")
public class FabricanteConverter implements Converter {

@Inject
FabricanteService fabricanteService;

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
    System.out.println("Valor = " + value);
    return (value != null && value.trim().length() > 0) ? fabricanteService.listarPorId(Long.parseLong(value)) : null;
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object obj) {
    return obj != null ? String.valueOf(((Fabricante) obj).getId()) : null;
}

}

// Calling convert to view

<p:selectOneMenu id="tipodocumento" value="#{cadastroDocumentoBean.documentoConceito.tipoDocumento}" >
<f:selectItem itemLabel="Selecione" noSelectionOption="true" />
<f:selectItems value="#{cadastroDocumentoBean.tiposDocumento}" var="tipoDocumento" itemValue="#{tipoDocumento}" itemLabel="#{tipoDocumento.descricao}"/>

Hope it helps

    
23.10.2016 / 00:59