0

I'm new in this community, but I really need help! I'm programming a application which gonna run in Desktop, with Java, and online, with Java Web, using Hibernate 4.3, without JPA, and Primefaces 8.2. My issue is, I have a page with a dataTable, this dataTable contains a button on each register to open a dialog with more information of this register. Inside this dialog i have another dataTable with another button inside each register. Here I have two issues.

First, when user clicks on the button in the first dialog, the second dialog appears but, it just ignore the ActionListener.

Second, on the first dialog, when user clicks to go to other page of dataTable, or just increase the number of rows, the data disappear! If you close and reopen the dialog, it open in the page you clicked before and the data will be there.

I'll post the principal code of the problem, the github link and a video to make it easy to figure out the problem.

Bean.java

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

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.event.ActionEvent;

import org.primefaces.model.LazyDataModel;

import com.sysnutriweb.dao.AlimentoXDietaDAO;
import com.sysnutriweb.dao.DietaDAO;
import com.sysnutriweb.dataModel.AlimentoDataModel;
import com.sysnutriweb.domain.Alimento;
import com.sysnutriweb.domain.AlimentoXDieta;
import com.sysnutriweb.domain.Dieta;

@ManagedBean(name = "MBDiet")
@SessionScoped
public class DietaBean implements Serializable {
    private static final long serialVersionUID = 1L;
    private List<Dieta> listDiets;
    private Dieta diet = new Dieta();
    private DietaDAO dDao;
    private LazyDataModel<AlimentoXDieta> foods;
    private Alimento food = new Alimento();

    @PostConstruct
    public void begin() {
        dDao = new DietaDAO();
        listDiets = dDao.loadAll();
    }

    public void getDietSelected(ActionEvent e) {
        diet = (Dieta) e.getComponent().getAttributes().get("selectedDiet");
        List<AlimentoXDieta> foods = new ArrayList<>();
        List<AlimentoXDieta> axd = new AlimentoXDietaDAO().loadAll();
        for (int i = 0; i < axd.size(); i++) {
            if (axd.get(i).getDietId().getId() == diet.getId())
                foods.add(axd.get(i));
        }
        this.foods = new AlimentoDataModel(foods);
    }

    public void getFoodSelected(ActionEvent e) {
        food = (Alimento) e.getComponent().getAttributes().get("selectedFood");
    }

    // Getters and Setters

Index.xhtml

<f:metadata>
    <f:param name="selectedDiet" value="#{MBDiet.diet}" />
</f:metadata>

<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Menu de dietas SysNutri</title>
</h:head>
<h:body>
    <p:layout fullPage="true">
        <p:layoutUnit position="north" size="100" resizable="false"
            closable="false" collapsible="false">
            <center>
                <h:outputText value="Sistema de Nutrição  SYSNUTRIWEB" />
            </center>
        </p:layoutUnit>
        <p:layoutUnit position="center">
            <h:form id="formTable">
                <p:dataTable emptyMessage="Nenhum dado cadastrado!"
                    paginator="false" id="tbAllDiets" value="#{MBDiet.listDiets}"
                    var="diet">
                    <p:column headerText="Nome" id="nome">
                        <p:outputLabel value="#{diet.nomeDieta}" />
                    </p:column>
                    <p:column headerText="Descrição da dieta" id="descricao">
                        <p:outputLabel value="#{diet.descricaoDieta}" />
                    </p:column>
                    <p:column headerText="Ações" id="acoes" width="10%">
                        <center>
                            <p:commandButton immediate="false" icon="ui-icon-info"
                                id="btnInfo" update=":formInfoTable"
                                actionListener="#{MBDiet.getDietSelected}"
                                oncomplete="PF('dlgFoods').show()">
                                <f:attribute name="selectedDiet" value="#{diet}" />
                            </p:commandButton>
                            <p:tooltip id="toolTipBtnInfo" for="btnInfo"
                                value="Clique para ver os alimentos presentes nesta dieta"
                                position="top" />
                        </center>
                    </p:column>
                </p:dataTable>
            </h:form>
        </p:layoutUnit>
    </p:layout>
    <!--  Dialogs -->
    <!--  Diet info -->
    <p:dialog header="Alimentos da dieta selecionada" widgetVar="dlgFoods"
        closable="true" modal="true" showEffect="explode" id="dialogFoods"
        resizable="false" draggable="false" appendTo="@(body)">
        <h:form id="formInfoTable">
            <center>
                <p:outputLabel id="diet" value="Dieta #{MBDiet.diet.nomeDieta}"
                    style="font-weight: bold;" />
            </center>
            <p:dataTable emptyMessage="Nenhum dado cadastrado!" paginator="true"
                id="tbFoodsDiet" value="#{MBDiet.foods}" var="food" rows="4"
                paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                rowsPerPageTemplate="4,5,6,7,8,9,10" lazy="true">
                <p:column headerText="Nome" id="nome" width="10px">
                    <p:outputLabel value="#{food.foodId.nome}" />
                </p:column>
                <p:column headerText="Hora para consumir" id="descricao" width="40%">
                    <p:outputLabel value="#{food.hora}" />
                </p:column>
                <p:column headerText="Ações" id="acoes" width="10%">
                    <center>
                        <p:commandButton immediate="false" icon="ui-icon-info"
                            id="btnInfo" update=":formInfo"
                            actionListener="#{MBDiet.getFoodSelected}"
                            oncomplete="PF('dlgFoodInfo').show()">
                            <f:attribute name="selectedFood" value="#{food}" />
                        </p:commandButton>
                        <p:tooltip id="toolTipBtnInfo" for="btnInfo"
                            value="Clique para ver os alimentos presentes nesta dieta"
                            position="top" />
                    </center>
                </p:column>
            </p:dataTable>
        </h:form>
    </p:dialog>
    <!--  Second Dialog -->

Github: https://github.com/SakamotoLfe/SysNutriWebV0

Vdieo: Coming soon.

Vasil Lukach
  • 3,658
  • 3
  • 31
  • 40
Alfredo Marin
  • 195
  • 1
  • 1
  • 14
  • 4
    Hi, Comple of remarks. 1: PrimeFaces 8.2 does not exist, 2: JSF questions are very, very rarely hibernate (or even jpa) related. Ususally when you narrow things down with a [mcve] it is either JSF or Hibernate/JPA related. 3: Read [ask] about your title and also read the 'tagging' link in there about titles, and also read https://stackoverflow.com/tags/jsf/info. And read https://stackoverflow.com/questions/14429729/injecting-beans-in-jsf-2-0 – Kukeltje Nov 26 '18 at 16:10
  • Thanks for the help! I saw the link you posted but, i'm not using CDI or any injected beans and i don't think i need to. I don't even know how to use. I need to finish this until 12/11/2018 so, i don't think i have time to learn about CDI and we don't have sure that's gonna solve the problem. About my title, it's better now? I really don't know, sorry for that and really thanks for your attention! – Alfredo Marin Nov 26 '18 at 16:45
  • Read the post again, it is not only directly abount injection. It islso about managed beans, cdi OR jsf ones. And you make an error thst is explained there... the imports 'same' error as in the first piece of code in the question but the other way around – Kukeltje Nov 26 '18 at 18:47
  • I get it!! It works!! The first issue is, wrong import. It has to be faces.bean Te second issue, i was casting to the wrong object and then we have a exception, already solved! Really Thanks!! I'll pay more attention on the imports. ... so now, how do I put this issue as a solved question? – Alfredo Marin Nov 26 '18 at 20:20
  • You don't need to. I'll mark it as a duplicate of the other one (and improve the title of the other one). But next time, please make a [mcve], also for yourself to narrow down things – Kukeltje Nov 26 '18 at 21:50
  • Thanks a lot! Next time i'll pay more attention on MCVE. Thanks! – Alfredo Marin Nov 28 '18 at 15:03

0 Answers0