Load objects from other classes

Asked

Viewed 199 times

0

I have a selectOneMenu which will be used to list all Generations (data in the database). These data should be listed when registering a Nature object. My selectOneMenu is 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 carry all the Generations in mine selectOneMenu ?

  • Have you looked at the example in the documentation? http://www.primefaces.org/showcase/ui/input/oneMenu.xhtml Can you detail which part of the example you don’t understand? And, man, take these off Try-catch from there, please!! hehehe

  • rsrsrs.... Which Try/catch ?

  • @Caffé I had even looked at this example...but I did not understand well the part to pull from the bank, etc., I created a Converter already.

  • Your question is how to pull from the bank or how to display on the screen? The page code seems correct. In Java, you have to do something like return geracoesDao.findAll() in the getGeracoes.

  • @Caffé right.... but in this case we do not use the convert ?

2 answers

0

By quickly analyzing your code, I realized that the search method has not been processed, it is only implemented. Invoke it within the start method, because I saw that you have a @Postconstruct or else on your screen insert an 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>

0

About the converter it is necessary due 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 convert, below is an example of the implementation of a convert and also its call in 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 the convert in 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}"/>

I hope it helps

  • Where he is called ?

Browser other questions tagged

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