0

I have the following files in my application but I can't get the values from the selectOneMenu on file elementoUpdateDialog.xhtml to update the dataTable on gerirElementos.xhtml and sql table.

Where is my error?

elementoUpdateDialog.xhtml

<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:body>
<p:dialog widgetVar="elementoUpdateDialogWidget"
    id="elementoUpdateDialogId" height="300" width="500" modal="true"
    closable="true" draggable="false" resizable="false">
    <h:form id="elementoUpdateDialogForm" prependId="false">
        <h:panelGrid columns="2">
            <h:outputText value="elementoID" />
            <h:inputText value="#{elementoMB.elemento.elementoId}"
                required="true" label="elementoID" />

            <h:outputText value="Posto" />
            <h:outputText value="#{elementoMB.elemento.posto.postoId}"         />               
            <h:selectOneMenu value="#{postoMB.posto.postoId}">
                <f:selectItems value="#{postoMB.allPostos}" var     ="posto"
                itemLabel="#{postoMB.posto.posto}" itemValue="#    {elementoMB.elemento.posto.postoId}"/>
            </h:selectOneMenu>
            <br />
            <h:outputText value="Classe" /> 
            <h:outputText value="#{elementoMB.elemento.classe.classeId}" />     
            <h:selectOneMenu value="#    {elementoMB.elemento.classe.classeId}">
                <f:selectItems value="#{classeMB.allClasses}"/>
                <f:selectItem itemValue="#    {elementoMB.elemento.classe.classeId}" itemLabel="#{elementoMB.elemento.classe.classeId}"/>
            </h:selectOneMenu>
            <br/>
            <h:outputText value="Nome" />
            <h:inputText value="#{elementoMB.elemento.nome}"     required="true"
                label="Nome" />
            <h:outputText value="NII" />
            <h:inputText value="#{elementoMB.elemento.NII}"     required="true"
                label="NII" />

            <p:commandButton value="Gravar" icon="ui-icon-plus"
                action="#{elementoMB.updateElemento()}"
                update=":messageGrowl     :elementosForm:elementosTable"
                oncomplete="closeDialogIfSucess(xhr, status, args,      elementoUpdateDialogWidget, 'elementoUpdateDialogId')" />
            <p:commandButton value="Cancelar" icon="ui-icon-cancel"
                actionListener="#{elementoMB.resetElemento()}"
                onclick="elementoUpdateDialogWidget.hide();"     type="button" />
        </h:panelGrid>
    </h:form>
</p:dialog>
</h:body>
</html>

gerirElementos.xhtml

<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>


</h:head>
<h:body>
<ui:composition template="/pages/protected/templates/master.xhtml">
    <ui:define name="divMain">
        <p:menubar>
            <p:submenu label="Elementos" icon="ui-icon-contact">
                <p:menuitem value="Listar"
                    url="/pages/protected/defaultUser/gerirElementos.xhtml" />
            </p:submenu>

            <p:submenu label="Classes" icon="ui-icon-contact">
                <p:menuitem value="Listar"
                    url="/pages/protected/admin/gerirClasses.xhtml" />
            </p:submenu>

            <p:submenu label="Postos" icon="ui-icon-contact">
                <p:menuitem value="Listar"
                    url="/pages/protected/admin/gerirPostos.xhtml" />
            </p:submenu>

            <p:submenu label="RegistoSarPerm" icon="ui-icon-contact">
                <p:menuitem value="Listar"
                    url="/pages/protected/defaultUser/gerirRegistoSarPerm.xhtml" />
            </p:submenu>


        </p:menubar>

        <h:form id="elementosForm">

            <p:dataTable id="elementosTable" var="elemento"
                value="#{elementoMB.allElementos}" paginator="true" rows="10"
                selepaginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                rowsPerPageTemplate="1,5,10">

                <f:facet name="header">  
        Lista de Elementos  
    </f:facet>

                <p:column headerText="NII" sortBy="nii" id="nii">  
        #{elemento.NII}  
    </p:column>

                <p:column headerText="Posto" sortBy="posto" id="posto">  
        #{elemento.posto.posto}  
    </p:column>

                <p:column headerText="Classe" sortBy="classe" id="classe">  
        #{elemento.classe.classe}  
    </p:column>

                <p:column headerText="Nome" sortBy="nome" id="nome">  
        #{elemento.nome}  
    </p:column>
                <p:column>

                    <p:spacer width="10px" />
                    <p:commandButton value="Editar" icon="ui-icon-pencil"
                        update=":elementoUpdateDialogForm"
                        onclick="elementoUpdateDialogWidget.show();">
                        <f:setPropertyActionListener target="#{elementoMB.elemento}"
                            value="#{elemento}" />
                    </p:commandButton>
                    <p:spacer width="10px" />
                    <p:commandButton value="Eliminar" icon="ui-icon-trash"
                        update=":elementoDeleteDialogForm"
                        onclick="elementoDeleteDialogWidget.show();">
                        <f:setPropertyActionListener target="#{elementoMB.elemento}"
                            value="#{elemento}" />
                    </p:commandButton>
                    <p:spacer width="10px" />

                </p:column>

            </p:dataTable>
            <p:commandButton value="Novo Elemento" icon="ui-icon-plus"
                actionListener="#{elementoMB.resetElemento()}"
                onclick="elementoCreateDialogWidget.show();" />
        </h:form>
        <ui:include
            src="/pages/protected/defaultUser/dialogs/elementoCreateDialog.xhtml" />
        <ui:include
            src="/pages/protected/defaultUser/dialogs/elementoUpdateDialog.xhtml" />
        <ui:include
                src="/pages/protected/defaultUser/dialogs/elementoDeleteDialog.xhtml" />
    </ui:define>
</ui:composition>
</h:body>
</html>

Elemento.java

package com.model;

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

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="Elemento")

public class Elemento implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="elementoId")
private int elementoId;

@ManyToOne
@JoinColumn(name = "classeId")
private Classe classe;

@ManyToOne
@JoinColumn(name="postoID")
private Posto posto;


@Column(name="NII")
private String NII;

@Column(name="nome")
private String nome;

public Elemento(){

}

public Elemento(String NII, String nome){
    this.NII = NII;
    this.nome = nome;
}

//Getters and Setters
public int getElementoId() {
    return elementoId;
}

public void setElementoId(int elementoId) {
    this.elementoId = elementoId;
}




public String getNII() {
    return NII;
}

public void setNII(String nII) {
    NII = nII;
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}



public Classe getClasse() {
    return classe;
}

public void setClasse(Classe classe) {
    this.classe = classe;
}

public Posto getPosto() {
    return posto;
}

public void setPosto(Posto posto) {
    this.posto = posto;
}

@Override
public int hashCode() {
    return elementoId;
}

@Override
public boolean equals(Object obj) {
    if (obj instanceof Elemento) {
        Elemento elemento = (Elemento) obj;
        return elemento.getElementoId() == elementoId;
    }

    return false;
}
}

ElementoMB.java

package com.mb;

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

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.facade.ElementoFacade;
import com.model.Classe;
import com.model.Elemento;
import com.model.Posto;

@ViewScoped
@ManagedBean
public class ElementoMB extends AbstractMB implements Serializable {
private static final long serialVersionUID = 1L;



private Posto posto;
private Classe classe;
private Elemento elemento;


private List<Elemento> elementos;

private ElementoFacade elementoFacade;

public void createElemento() {
    try {
        getElementoFacade().createElemento(elemento);;
        closeDialog();
        displayInfoMessageToUser("Created With Sucess");
        loadElementos();
        resetElemento();
    } catch (Exception e) {
        keepDialogOpen();
        displayErrorMessageToUser("Ops, we could not create. Try again later");
        e.printStackTrace();
    }
}

public void updateElemento() {
    try {
        getElementoFacade().updateElemento(elemento);
        closeDialog();
        displayInfoMessageToUser("Updated With Sucess");
        loadElementos();
        resetElemento();
    } catch (Exception e) {
        keepDialogOpen();
        displayErrorMessageToUser("Ops, we could not create. Try again later");
        e.printStackTrace();
    }
}

public void deleteElemento() {
    try {
        getElementoFacade().deleteElemento(elemento);
        closeDialog();
        displayInfoMessageToUser("Deleted With Sucess");
        loadElementos();
        resetElemento();
    } catch (Exception e) {
        keepDialogOpen();
        displayErrorMessageToUser("Ops, we could not create. Try again later");
        e.printStackTrace();
    }
}

public List<Elemento> getAllElementos(){
    if (elementos == null){
        loadElementos();
    }
    return elementos;
}

public ElementoFacade getElementoFacade() {
    if (elementoFacade == null) {
        elementoFacade = new ElementoFacade();
    }

    return elementoFacade;
}

public Elemento getElemento() {
    if (elemento == null) {
        elemento = new Elemento();
    }

    return elemento;
}

public void setElemento(Elemento elemento) {
    this.elemento = elemento;
}



private void loadElementos() {
    elementos = getElementoFacade().listAll();
}



public void resetElemento() {
    elemento = new Elemento();
}



public Posto getPosto() {
    if (posto == null){
        posto = new Posto();
    }
    return posto;
}

public void setPosto(Posto posto) {
    this.posto = posto;
}

public Classe getClasse() {
    if (classe == null) {
        classe = new Classe();
    }

    return classe;
}

public void setClasse(Classe classe) {
    this.classe = classe;
}

public void resetClasse() {
    classe = new Classe();
}


}

ElementoFacade.java

package com.facade;

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

import com.dao.ElementoDAO;
import com.model.Elemento;

public class ElementoFacade implements Serializable {
private static final long serialVersionUID = 1L;

private ElementoDAO elementoDAO = new ElementoDAO();
public void createElemento(Elemento elemento) {
    elementoDAO.beginTransaction();
    elementoDAO.save(elemento);
    elementoDAO.commitAndCloseTransaction();
}

public void updateElemento(Elemento elemento) {
    elementoDAO.beginTransaction();
    Elemento persistedElemento = elementoDAO.find(elemento.getElementoId());
    persistedElemento.setNome(elemento.getNome());
    persistedElemento.setNII(elemento.getNII());
    elementoDAO.commitAndCloseTransaction();
}

public void deleteElemento(Elemento elemento){
    elementoDAO.beginTransaction();
    Elemento persistedElementoWithIdOnly = elementoDAO.findReferenceOnly(elemento.getElementoId());
    elementoDAO.delete(persistedElementoWithIdOnly);
    elementoDAO.commitAndCloseTransaction();

}

public Elemento findElemento(int elementoId) {
    elementoDAO.beginTransaction();
    Elemento elemento = elementoDAO.find(elementoId);
    elementoDAO.closeTransaction();
    return elemento;
}

public List<Elemento> listAll() {
    elementoDAO.beginTransaction();
    List<Elemento> result = elementoDAO.findAll();
    elementoDAO.closeTransaction();
    return result;
}


}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

1 Answers1

1

Here,

<f:selectItems value="#{postoMB.allPostos}" var="posto"
    itemLabel="#{postoMB.posto.posto}" itemValue="#{elementoMB.elemento.posto.postoId}"/>

Your itemLabel and itemValue attributes, representing the current option label and value, are wrong. They're for some unclear reason referencing a backing bean property instead of the currently iterated option object as definied in var="posto".

Fix it accordingly:

<f:selectItems value="#{postoMB.allPostos}" var="posto"
    itemLabel="#{posto.postoId}" itemValue="#{posto}"/>

This should display the items correctly.

And here,

<h:selectOneMenu value="#{postoMB.posto.postoId}">
      

the selected item is wrong. This would only cause problems during submitting and during displaying a preselected item. You should refer the very same type as itemValue itself, not some ID (otherwise you're changing only the id of an entity instead of the entity itself, resulting in major potential trouble as those are references):

<h:selectOneMenu value="#{postoMB.posto}">

Supply if necessary a converter in case you don't have a @FacesConverter(forClass=Posto.class).

Apply the same lesson learnt on the other <h:selectOneMenu> you've there.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555