Java Web with Primefaces and Hibernate

Asked

Viewed 65 times

-2

I have problems with the Primefaces Dialog. I have a Dialog that will appear when I click on a commandButton, in the oncomplete method, of a datatable. This Dialog will have another datatable that will also have another button to open another dialog, this time with another datatable and nothing else. My problem is, the first Dialog does not pull the objects correctly, as you can see in the code, the button calls the method in actionListener but the dialog seems to open before executing actionListener and opens with the objects still empty.


package com.sysnutriweb.bean;

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 javax.inject.Named;

import com.sysnutriweb.dao.Alimentoxdietadao; import com.sysnutriweb.dao.Dietadao; import com.sysnutriweb.Domain.Food; import with.sysnutriweb.Domain.Alimentoxdiet; import with.sysnutriweb.Omain.Diet;

@Named(value = "Mbdiet") @Managedbean(name = "Mbdiet") @Sessionscoped public class Dietabean Implements Serializable { /** * */ private Static final long serialVersionUID = 1L; Private list listDiets; private Diet = new Diet(); private dietadao dDao; private list Foods = new Arraylist<>(); private food;

private String flagS = "V-";

@PostConstruct
public void begin() {
    dDao = new DietaDAO();
    listDiets = dDao.loadAll();
    flagS += "B-";
}

//@PostConstruct
public void getDietSelected(ActionEvent e) {
    diet = (Dieta) e.getComponent().getAttributes().get("selectedDiet");
    System.out.println("Diet: " + diet.toString());
    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));
    }
    flagS+="S-";
    System.out.println("Called: " + foods.size() + "\nFlagS: " + flagS);
}

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

//Getters Setters
}

And here’s the troublesome part of my XHTML.



            
                
                    
                        
                    

                <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 icon="ui-icon-info" id="btnInfo" update="@form"
                            actionListener="#{MBDiet.getDietSelected}"
                            oncomplete="PF('dlgFoods').show()" ajax="true">
                            <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>

This part of the code calls the dialog described here

<!--  Diet info -->
<p:dialog
    header="Alimentos na dieta #{MBDiet.flagS} #{MBDiet.diet.nomeDieta}"
    widgetVar="dlgFoods" modal="true" showEffect="explode"
    resizable="false" draggable="false" appendTo="@(body)">
    <h:form id="formInfoTable">
        <p:dataTable emptyMessage="Nenhum dado cadastrado!" paginator="true"
            id="tbFoodsDiet" value="#{MBDiet.foods}" var="food">
            <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 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 detalhes deste alimento" position="top" />
                </center>
            </p:column>
        </p:dataTable>
    </h:form>
</p:dialog>

That soon after will call this dialog

<!--  Food info -->
<p:dialog header="Alimento #{MBDiet.food.nome}" widgetVar="dlgFoodInfo"
    modal="true" showEffect="fade" height="100" resizable="false" id="dlgFoodInfo"
    draggable="true" appendTo="@(body)">
    <h:form id="formInfo">
        <h:panelGrid columns="2">
            <p:outputLabel value="Nome:" />
            <p:outputLabel value="#{MBDiet.food.nome}" />

            <p:outputLabel value="Açucares:" />
            <p:outputLabel value="#{MBDiet.food.acucares}" />

            <p:outputLabel value="Cálcio:" />
            <p:outputLabel value="#{MBDiet.food.calcio}" />

            <p:outputLabel value="Calorias:" />
            <p:outputLabel value="#{MBDiet.food.calorias}" />

            <p:outputLabel value="Carboidratos:" />
            <p:outputLabel value="#{MBDiet.food.carboidratos}" />

            <p:outputLabel value="Colesterol:" />
            <p:outputLabel value="#{MBDiet.food.colesterol}" />

            <p:outputLabel value="Ferro:" />
            <p:outputLabel value="#{MBDiet.food.ferro}" />

            <p:outputLabel value="Fibras:" />
            <p:outputLabel value="#{MBDiet.food.fibras}" />

            <p:outputLabel value="Gorduras:" />
            <p:outputLabel value="#{MBDiet.food.gorduras}" />

            <p:outputLabel value="Insaturadas:" />
            <p:outputLabel value="#{MBDiet.food.insaturadas}" />

            <p:outputLabel value="pootássio:" />
            <p:outputLabel value="#{MBDiet.food.potassio}" />

            <p:outputLabel value="Proteínas:" />
            <p:outputLabel value="#{MBDiet.food.proteinas}" />

            <p:outputLabel value="Saturadas:" />
            <p:outputLabel value="#{MBDiet.food.saturadas}" />

            <p:outputLabel value="Trans:" />
            <p:outputLabel value="#{MBDiet.food.trans}" />

            <p:outputLabel value="Vitamina A:" />
            <p:outputLabel value="#{MBDiet.food.vitaminaA}" />

            <p:outputLabel value="Vitamina C:" />
            <p:outputLabel value="#{MBDiet.food.vitaminaC}" />

            <p:outputLabel value="Vitamina D:" />
            <p:outputLabel value="#{MBDiet.food.vitaminaD}" />
        </h:panelGrid>
        <p:separator />
        <p:outputLabel value="Informações especiais" />
        <p:separator />
        <h:panelGrid columns="2">
            <p:outputLabel value="Data de cadastro:" />
            <p:outputLabel value="#{MBDiet.food.dtCad}" />

            <p:outputLabel value="Identificação:" />
            <p:outputLabel value="#{MBDiet.food.id}" />
        </h:panelGrid>
    </h:form>
</p:dialog>

If you prefer, I will leave the github link and I apologize, this is my first question! From now on, thank you all! Link: https://github.com/SakamotoLfe/SysNutriWebV0

  • 1

    Welcome to Stackoverflow Alfredo. Always post the code directly on the question, because in the future if the link breaks your question will no longer be useful. You can even link to the full code on Github, but put at least the problematic part in the question. I suggest you read this help article from the site: How to create a Minimum, Complete and Verifiable example.

  • 1

    Alfredo, unfortunately no one will navigate your project in search of your problem and you will certainly not get the help you need, besides having your question closed by moderation. The usual is you edit your question and put in it the problematic code snippet, pointing out what you have already tried to solve the problem and indicating the error (the Exception or the error message) that happened.

  • Alfredo the goal here is not to teach a whole content and yes to ask questions, post the excerpt you are doubtful. Welcome to the community.

1 answer

0


I discovered the error. In fact it was all working correctly, only the Dialog that did not display the data I wanted in your Header. After changing Commandbutton Update everything worked normally!

Browser other questions tagged

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