Doubt in listing data from the JAVA database - JSF

Asked

Viewed 611 times

0

Good afternoon, I have a doubt in listing data in a datatable, coming from the BD. I do not know how to return to the BD data view. Follow my code until the moment

package br.com.racionalgames.pta.mbean;

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

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import br.com.racionalgames.pta.dao.ListPlanilhasDao;
import br.com.racionalgames.pta.model.UploadFiles;

@ManagedBean(name = "listPlanilhas")
public class ListPlanilhasMBean implements Serializable {

private static final long serialVersionUID = 1L;
private String checkOption;

private ArrayList<UploadFiles> uploadList = new ArrayList<UploadFiles>();

@ManagedProperty(value = "#{listPlanilhaDao}")
private List<ListPlanilhasDao> listPlanilha;

public List<ListPlanilhasDao> getListPlanilha() {
    return listPlanilha;
}

public void setListPlanilha(List<ListPlanilhasDao> listPlanilha) {
    this.listPlanilha = listPlanilha;
}

public ArrayList<UploadFiles> getUploadList() {
    return uploadList;
}

public void setUploadList(ArrayList<UploadFiles> uploadList) {
    this.uploadList = uploadList;
}

public ListPlanilhasMBean() {

}

public String getCheckOption() {
    return checkOption;
}

public void setCheckOption(String checkOption) {
    this.checkOption = checkOption;
}

public List<UploadFiles> getListPlanilha(UploadFiles upload) {

    List<UploadFiles> list = null;
    try {
        list = ((ListPlanilhasDao) listPlanilha).getListPlanilha(upload);
    } catch (Exception e) {

        e.printStackTrace();
    }

    return list;

}

}

/DAO/

package br.com.racionalgames.pta.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

import br.com.racionalgames.pta.model.UploadFiles;

@Repository("ListPlanilhasDao")
@Transactional
@EnableTransactionManagement

public class ListPlanilhasDaoImpl implements ListPlanilhasDao {

    private static final long serialVersionUID = 1L;

    private UploadFiles listPlanilha = new UploadFiles();

    public UploadFiles getUpload() {
        return listPlanilha;
    }

    public void setUpload(UploadFiles listPlanilha) {
        this.listPlanilha = listPlanilha;
    }

    @Override
    public List<UploadFiles> getListPlanilha(UploadFiles a) throws Exception {

        String sql = "SELECT * FROM pta.criterios_multiskill";

        List<UploadFiles> list = new ArrayList<UploadFiles>();

        list = (List<UploadFiles>) sessionFactory.getCurrentSession().createSQLQuery(sql).list();

        return list;
    }

    @Autowired
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

}

/VIEW/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:f="http://xmlns.jcp.org/jsf/core">
<ui:composition template="/WEB-INF/inc/template.xhtml">
    <ui:define name="titulo">
        <h:outputText value=" - Home" />
    </ui:define>
    <ui:define name="conteudo">
        <div class='wrp-content'>
            <h:form style="margin-top: 100px">
                <p:selectOneMenu style="width: 30%" id="planilha" effect="fold"
                    editable="false" value="">
                    <f:selectItem id="multiSkill" value="#{listPlanilhas.checkOption}"
                        itemLabel="Critérios Cadastro MultiSkill" />
                    <f:selectItem id="compUsuario" value="#{listPlanilhas.checkOption}"
                        itemLabel="Publicação Complemento Usuário" />
                </p:selectOneMenu>
                <br />
                <br />
                <p:dataTable emptyMessage="Não existem valores para exibição"
                    var="lista" value="#{listPlanilhas.uploadList}" paginator="true"
                    rows="10" rowsPerPageTemplate="5, 10, 20">
                    <p:column headerText="ID">
                        <h:outputText value="#{listPlanilhas.listPlanilha.id}" />
                    </p:column>
                    <p:column headerText="Documento">
                        <h:outputText value="#{listPlanilhas.listPlanilha}" />
                    </p:column>
                    <p:column headerText="Nome">
                        <h:outputText value="#{listPlanilhas.listPlanilha}" />
                    </p:column>
                    <p:column headerText="Empresa Cliente">
                        <h:outputText value="#{listPlanilhas.listPlanilha}" />
                    </p:column>
                    <p:column headerText="Empresa Assessoria">
                        <h:outputText value="#{listPlanilhas.listPlanilha}" />
                    </p:column>
                    <p:column headerText="Carteira">
                        <h:outputText value="#{listPlanilhas.listPlanilha}" />
                    </p:column>
                    <p:column headerText="Produto">
                        <h:outputText value="#{listPlanilhas.listPlanilha}" />
                    </p:column>
                    <p:column headerText="Tipo Operação">
                        <h:outputText value="#{listPlanilhas.listPlanilha}" />
                    </p:column>
                    <p:column headerText="Grupo">
                        <h:outputText value="#{listPlanilhas.listPlanilha}" />
                    </p:column>
                    <p:column headerText="Ação">
                        <h:outputText value="#{listPlanilhas.listPlanilha}" />
                    </p:column>
                    <p:column headerText="Data Inclusão">
                        <h:outputText value="#{listPlanilhas.listPlanilha}" />
                    </p:column>
                </p:dataTable>
            </h:form>
        </div>
    </ui:define>
</ui:composition>
</html>

1 answer

1

<p:dataTable emptyMessage="Não existem valores para exibição"
                    var="lista" value="#{listPlanilhas.uploadList}" paginator="true"
                    rows="10" rowsPerPageTemplate="5, 10, 20">
                    <p:column headerText="ID">
                        <h:outputText value="#{lista.id}" />
                    </p:column>
                    <p:column headerText="Documento">
                        <h:outputText value="#{lista.documento}" />
                    </p:column>
                    <p:column headerText="Nome">
                        <h:outputText value="#{lista.nome}" />
                    </p:column>


                </p:dataTable>

you have to access your variable that is in your var="lista" that its object #{list.name}.

see in a basic example of the primefaces.

**.Xhtml**

<p:column headerText="Year">
    <h:outputText value="#{car.year}" />
</p:column>

<p:column headerText="Brand">
    <h:outputText value="#{car.brand}" />
</p:column>

<p:column headerText="Color">
    <h:outputText value="#{car.color}" />
</p:column>

**Beans**

@ManagedBean(name="dtBasicView")
@ViewScoped
public class BasicView implements Serializable {

    private List<Car> cars;

    @ManagedProperty("#{carService}")
    private CarService service;

    @PostConstruct
    public void init() {
        cars = service.createCars(10);
    }

    public List<Car> getCars() {
        return cars;
    }

    public void setService(CarService service) {
        this.service = service;
    }
}

see that public getCars() is the list of cars from the bank.

  var="car" value="#{dtBasicView.cars}"

From the variable car you can access the object car and his properties in .xhtml as an example car.id , car.year. In your house is

#{lista.nome}
  • Hello good morning. My doubt this, if my logic is correct, this is my method that calls DAO public List<Uploadfiles> getListPlanilha(Uploadfiles listaPlanilha) { List<Uploadfiles> list = null; Try { list = ((Listsheet) listPlanilha). getListPlanilha(listPlanilha); } catch (Exception e) { System.out.print("Error: " + e); } Return list; }

  • And this is my DAO that does the search, but I do not know if I am making the addition in the list of data table @Override public List<Uploadfiles> getListPlanilla(Uploadfiles a) throws Exception { Query list = null; String sql = "SELECT * FROM Pta.criterios_multiskill"; list = sessionFactory.getCurrentSession(). createQuery(sql); Return (List<Uploadfiles>) list; }

  • he is not listing the data /

  • not this, can not find the list data in view

  • what ? name of your entity ?

  • My entity that I want to perform the search is Uploadfiles I’m calling it as list la no bean private List<Uploadfiles> listPlanilha = new Arraylist<Uploadfiles>();

  • Uploadfiles which tabla in your comic represents her ?

  • The Uploadfiles entity table is called criterios_multiskill. I have the DAO ready for it, I just don’t know if I did it right @Override public List<Uploadfiles> getListPlanilla(Uploadfiles a) throws Exception { List<Uploadfiles> list = new Arraylist<Uploadfiles>(); Query list = null; String sql = "SELECT * FROM Pta.criterios_multiskill"; list = (Query) sessionFactory.getCurrentSession(). createSQLQuery(sql). list(); Return (List<Uploadfiles>) list; }

  • in the log appears something ? Some mistake ?

Show 4 more comments

Browser other questions tagged

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