-1

hello everyone I'm trying to build a webpage with two oneMenu the second one depending on the second one. My XHTML file is below as well as my bean. when I try to create a new "structureAttache" I have the following problem

TemplateStructureAttache.xhtml @40,139 value="#{structureBean.structureAttache.intituleStructure}": Target Unreachable, 'structureAttache' returned null

and when I use the method initStruc to create a new structureAttache then I call it in the listener of my first ajax, I succeed to create a structureAttache but when I do it for the second time, it seems like the value already exit in the DB and it refuses please help me to solve this issue

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dresen.dresen.Beans;

import com.dresen.dresen.ServiceInterface.IArrondissementService;
import com.dresen.dresen.ServiceInterface.IStructureService;
import com.dresen.dresen.entities.Arrondissement;
import com.dresen.dresen.entities.StructureAttache;
import com.dresen.dresen.entities.CategorieStructure;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import com.dresen.dresen.ServiceInterface.ICategorieStructureService;
import com.dresen.dresen.ServiceInterface.IDepartementService;
import com.dresen.dresen.entities.Departement;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author Vivien Saa
 */
@ManagedBean
@ViewScoped
public class StructureBean implements Serializable{
    @ManagedProperty(value = "#{IStructureService}")
    private IStructureService iStructureService;

    @ManagedProperty(value = "#{IArrondissementService}")
    private IArrondissementService iArrondissementService;

    @ManagedProperty(value = "#{ICategorieStructureService}")
    private ICategorieStructureService iCategorieStructureService;

    @ManagedProperty(value  ="#{IDepartementService}")
    private IDepartementService iDepartementService;


    private long idDepartement;
    private long idArrondissement;
    private long idCategorieStructure;
    private StructureAttache structureAttache;
    private List<Arrondissement> listArrondissement;
    private List<Departement> listDepartement;
    private List<CategorieStructure> listCategorieStructure;
    private Arrondissement arrondissement = new Arrondissement();
    private CategorieStructure CategorieStructure = new CategorieStructure();



    public StructureBean() {
        structureAttache = new StructureAttache();
        idDepartement = 0L;
        idArrondissement = 0L;
        idCategorieStructure = 0L;
    }

    public ICategorieStructureService getiCategorieStructureService() {
        return iCategorieStructureService;
    }

    public void setiCategorieStructureService(ICategorieStructureService iCategorieStructureService) {
        this.iCategorieStructureService = iCategorieStructureService;
    }

    public long getIdDepartement() {
        return idDepartement;
    }

    public void setIdDepartement(long idDepartement) {
        this.idDepartement = idDepartement;
    }

    public IDepartementService getiDepartementService() {
        return iDepartementService;
    }

    public void setiDepartementService(IDepartementService iDepartementService) {
        this.iDepartementService = iDepartementService;
    }

    public List<Departement> getListDepartement() {
        return iDepartementService.findAllDepartement();
    }

    public void setListDepartement(List<Departement> listDepartement) {
        this.listDepartement = listDepartement;
    }



    public long getIdCategorieStructure() {
        return idCategorieStructure;
    }

    public void setIdCategorieStructure(long idCategorieStructure) {
        this.idCategorieStructure = idCategorieStructure;
    }

    public List<CategorieStructure> getListCategorieStructure() {
        return iCategorieStructureService.findAllCategorieStructure();
    }

    public void setListCategorieStructure(List<CategorieStructure> listCategorieStructure) {
        this.listCategorieStructure = listCategorieStructure;
    }



    public IStructureService getiStructureService() {
        return iStructureService;
    }

    public void setiStructureService(IStructureService iStructureService) {
        this.iStructureService = iStructureService;
    }

    public IArrondissementService getiArrondissementService() {
        return iArrondissementService;
    }

    public void setiArrondissementService(IArrondissementService iArrondissementService) {
        this.iArrondissementService = iArrondissementService;
    }

    public long getIdArrondissement() {
        return idArrondissement;
    }

    public void setIdArrondissement(long idArrondissement) {
        this.idArrondissement = idArrondissement;
    }

    public StructureAttache getStructureAttache() {
        return structureAttache;
    }

    public void setStructureAttache(StructureAttache structureAttache) {
        this.structureAttache = structureAttache;
    }

    public List<Arrondissement> getListArrondissement() {
        return iArrondissementService.findArrondissementByIdDepart(idDepartement);
    }

    public void setListArrondissement(List<Arrondissement> listArrondissement) {
        this.listArrondissement = listArrondissement;
    }

    public Arrondissement getArrondissement() {
        return arrondissement;
    }

    public void setArrondissement(Arrondissement arrondissement) {
        this.arrondissement = arrondissement;
    }

    public CategorieStructure getCategorieStructure() {
        return CategorieStructure;
    }

    public void setCategorieStructure(CategorieStructure CategorieStructure) {
        this.CategorieStructure = CategorieStructure;
    }
    public void initStruc() {
        structureAttache = new StructureAttache();
    }

    public StructureAttache createStructure(){
        System.out.println("vvsssssssssvsssssssssssssssssssssssvv why don't you work");
        arrondissement = iArrondissementService.findArrondissementById(idArrondissement);
        CategorieStructure = iCategorieStructureService.findCategorieStructureById(idCategorieStructure);
        structureAttache.setCategorieStructure(CategorieStructure);
        structureAttache.setArrondissement(arrondissement);
        return iStructureService.createStructureAttache(structureAttache);
    }
    public StructureAttache findStructureById(){
        return iStructureService.findStructureAttacheById(structureAttache.getId());
    } 
    public StructureAttache updateStructure(){
        arrondissement = iArrondissementService.findArrondissementById(idArrondissement);
        CategorieStructure = iCategorieStructureService.findCategorieStructureById(idCategorieStructure);
        structureAttache.setCategorieStructure(CategorieStructure);
        structureAttache.setArrondissement(arrondissement);
        return iStructureService.updateStructureAttache(structureAttache);
    }
    public List<StructureAttache> findAllStructure(){
        return iStructureService.findAllStructureAttache();
    } 
}

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title>Application de gestion du personnel des services déconcentrés du MINESEC EXTREME NORD</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </h:head>
     <h:body> 
        <div id="menu">
            <ui:include src="Menu.xhtml"/>
        </div>
         <f:view>
            <p:dialog widgetVar="dlg" header=" Enregistrer une nouvel nouvelle Structure d'attache " hideEffect="explode" showEffect="explode" modal="true">
                <h:form id="formAjouter" >
                     <p:panelGrid id="panelAjouter" columns="2">
                                <p:outputLabel value="Département:" />       
                                <p:selectOneMenu id="depart" value="#{structureBean.idDepartement}"  label="programme"  filter="true" filterMatchMode="startsWith" panelStyle="width:220px">
                                    <p:ajax  event="change" update="arrond" />
                                    <f:selectItem itemLabel="Selectioner le département" itemValue="" noSelectionOption="true" />
                                    <f:selectItems var="custe1" value="#{structureBean.listDepartement}"   itemLabel="#{custe1.intituleDepartement}" itemValue="#{custe1.id}" itemLabelEscaped="true" />
                                </p:selectOneMenu>
                                <p:outputLabel value="Arrondissement:" />       
                                <p:selectOneMenu id="arrond" value="#{structureBean.idArrondissement}" filter="true" filterMatchMode="startsWith" panelStyle="width:220px">
                                    <f:selectItems var="custe2" value="#{structureBean.listArrondissement}"   itemLabel="#{custe2.intituleArrondissement}" itemValue="#{custe2.id}" itemLabelEscaped="true" />
                                </p:selectOneMenu>
                                 <p:outputLabel value="Categorie de Structure:" />       
                                 <p:selectOneMenu  value="#{structureBean.idCategorieStructure}" filter="true" filterMatchMode="startsWith" panelStyle="width:220px">
                                     <f:selectItems var="custe3" value="#{structureBean.listCategorieStructure}"   itemLabel="#{custe3.intituleCategorieStructure}" itemValue="#{custe3.id}" itemLabelEscaped="true" />
                                </p:selectOneMenu>
                                <p:outputLabel value="Intitule :" for="intitule" />
                                <p:inputText id="intitule" value="#{structureBean.structureAttache.intituleStructure}" title="intitulé" />
                                <p:outputLabel value="Code/Abréviation :" for="abrev" />
                                <p:inputText id="abrev" value="#{structureBean.structureAttache.codeStructure}" title="abrev"/>
                                <p:commandButton value="Enregistrer" action="#{structureBean.createStructure()}"  oncomplete="PF('dlg').hide()" update=":tableForm:table" id="bout1" ajax="false" />
                    </p:panelGrid>


                </h:form>
            </p:dialog>
            <p:dialog widgetVar="dl" header=" Modifier une structure d'attache" hideEffect="fold" showEffect="explode" resizable="true">
                <h:form id="formModifier" enctype="multipart/form-data">                
                    <p:panelGrid id="panelGModifier" columns="2">
                                <p:outputLabel value="Département:" />       
                                <p:selectOneMenu id="depart" value="#{structureBean.idDepartement}"  label="programme"  filter="true" filterMatchMode="startsWith" panelStyle="width:220px">
                                    <p:ajax  event="change" update="arrond" />
                                    <f:selectItem itemLabel="Selectioner le département" itemValue="" noSelectionOption="true" />
                                    <f:selectItems var="custe1" value="#{structureBean.listDepartement}"   itemLabel="#{custe1.intituleDepartement}" itemValue="#{custe1.id}" itemLabelEscaped="true" />
                                </p:selectOneMenu>
                                <p:outputLabel value="Arrondissement:" />       
                                <p:selectOneMenu id="arrond" value="#{structureBean.idArrondissement}" filter="true" filterMatchMode="startsWith" panelStyle="width:220px">
                                    <f:selectItems var="custe2" value="#{structureBean.listArrondissement}"   itemLabel="#{custe2.intituleArrondissement}" itemValue="#{custe2.id}" itemLabelEscaped="true" />
                                </p:selectOneMenu>
                                 <p:outputLabel value="Categorie de Structure:" />       
                                 <p:selectOneMenu  value="#{structureBean.idCategorieStructure}" filter="true" filterMatchMode="startsWith" panelStyle="width:220px">
                                     <f:selectItems var="custe3" value="#{structureBean.listCategorieStructure}"   itemLabel="#{custe3.intituleCategorieStructure}" itemValue="#{custe3.id}" itemLabelEscaped="true" />
                                </p:selectOneMenu>
                                <p:outputLabel value="Intitule :" for="intitule" />
                                <p:inputText id="intitule" value="#{structureBean.structureAttache.intituleStructure}" title="intitulé" />
                                <p:outputLabel value="Code/Abréviation :" for="abrev" />
                                <p:inputText id="abrev" value="#{structureBean.structureAttache.codeStructure}" title="abrev"/>
                                <h:inputHidden id="number" value="#{structureBean.structureAttache.id}" />
                                <p:commandButton value="Modifier" action="#{structureBean.updateStructure()}"  oncomplete="PF('dl').hide()" update=":tableForm:table" id="bout1" ajax="false" />
                    </p:panelGrid>
                </h:form>
            </p:dialog> 
        </f:view>


        <f:view>
            <h:form id='tableForm'>

                <p:dataTable value="#{structureBean.findAllStructure()}" var="item" paginator="true" rows="10" paginatorTemplate=" {CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown} " id="table" rowsPerPageTemplate="5,10,15" selectionMode="single" selection="#{structureBean.structureAttache}" rowKey="#{item.id}">
                             <f:facet name="header">
                                La liste des Structures d'attache
                             </f:facet>

                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Structure"/>
                                </f:facet>
                                <h:outputText value="#{item.intituleStructure}"/>
                            </p:column>
                           <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Code/Abréviation Structure"/>
                                </f:facet>
                                <h:outputText value="#{item.codeStructure}"/>
                            </p:column>

                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Arrondissement"/>
                                </f:facet>
                                <h:outputText value="#{item.arrondissement.intituleArrondissement}"/>
                            </p:column>

                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Département"/>
                                </f:facet>
                                <h:outputText value="#{item.arrondissement.departement.intituleDepartement}"/>
                            </p:column>


                </p:dataTable>
                <p:toolbar>
                    <f:facet name="left">
                        <p:commandButton type="push" onclick="PF('dlg').show();" value="Nouvelle Structure" icon="ui-icon-disk">
                            <p:ajax update=":formAjouter:panelAjouter" resetValues="true" />
                        </p:commandButton>
                        <p:commandButton  onclick="PF('dl').show()" value="Modifier Structure" update=":formModifier:panelGModifier" icon="ui-icon-arrowrefresh-1-w"/>

                        <span class="ui-separator">
                            <span class="ui-icon ui-icon-grip-dotted-vertical" />
                        </span>
                        <p:commandButton type="push" title="Save" image="ui-icon-disk" />
                        <p:commandButton type="push" title="Update" icon="ui-icon-arrowrefresh-1-w"/>
                        <p:commandButton type="push" title="Print" image="ui-icon-print"/>
                    </f:facet>
                </p:toolbar>
            </h:form>
        </f:view>
     </h:body>
</html>
tfosra
  • 581
  • 5
  • 12
Vivien SA'A
  • 727
  • 8
  • 16

1 Answers1

0

You can use a method to initialize your attributes as you would like to, then through an AJAX or and actionListener, you call the method that will do the job before you try to use the attribute.

Vivien SA'A
  • 727
  • 8
  • 16