dlahlou
dlahlou

Reputation: 43

Primefaces message not showing

I'm writing this question after trying many solutions, but they are not working

I'm developping an application to read a card (with javaCard). In case no card is detected, I need to show a message to the user to tell him so.

here is my xhtml file : interogerPC.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui" template="/templates/template.xhtml">

    <ui:define name="title">M2M Group</ui:define>

    <ui:define name="header">
        <ui:include src="/templates/headers/header.xhtml" />
    </ui:define>

    <ui:define name="sider">
        <ui:include src="./sider.xhtml" />
    </ui:define>

    <ui:define name="body">

        <object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
            codebase="http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0">
            <param name="CODE" value="com.m2m.utils.AppletWeb.class" />
            <param name="type" value="application/x-java-applet" />
            <param name="scriptable" value="true" />
            <param name="ARCHIVE" value="../../AppletWeb.jar" />
            <param name="permissions" value="all-permissions" />
            <embed type="application/x-java-applet" hidden="true"
                code="com.m2m.utils.AppletWeb.class" archive="../../AppletWeb.jar"
                scriptable="true"
                pluginspage="http://java.sun.com/products/plugin/1.3/plugin-install.html" />
        </object>

        <h:form id="interogerPC">
            <div align="center">
                <p:panel header="Données Permis de Conduire">

                    <p:messages id="messages" showDetail="true" autoUpdate="true"
                        closable="true" />

                    <h:panelGrid columns="2" cellspacing="5">

                        <p:outputLabel for="nom_ar" value="Nom Arabe :" />
                        <p:inputText id="nom_ar" value="#{permisView.permis.nomArabe}" />

                        <p:outputLabel for="prenom_ar" value="Prénom arabe :" />
                        <p:inputText id="prenom_ar"
                            value="#{permisView.permis.prenomArabe}" />

                        <p:outputLabel for="nom_fr" value="Nom Français :" />
                        <p:inputText id="nom_fr" value="#{permisView.permis.nom}" />

                        <p:outputLabel for="prenom_fr" value="Prenom Français :" />
                        <p:inputText id="prenom_fr" value="#{permisView.permis.prenom}" />

                        <p:outputLabel for="cin" value="CIN :" />
                        <p:inputText id="cin" value="#{permisView.permis.cin}" />

                        <p:outputLabel for="adresse" value="Adresse :" />
                        <p:inputTextarea id="adresse" rows="3"
                            value="#{permisView.permis.adresse}" />

                        <p:outputLabel for="date_naissance" value="Date Naissance :" />
                        <p:calendar id="date_naissance" pattern="dd/MM/yyyy"
                            value="#{permisView.permis.dateNaissance}" />

                        <p:outputLabel for="lieu_naissance" value="Lieu Naissance :" />
                        <p:inputText id="lieu_naissance"
                            value="#{permisView.permis.lieuNaissance}" />

                        <p:outputLabel for="date_delivrance" value="Date Delivrance :" />
                        <p:calendar id="date_delivrance" pattern="dd/MM/yyyy"
                            value="#{permisView.permis.dateDelivrance}" />


                        <p:outputLabel for="pc" value="Numéro Permis :" />
                        <p:inputText id="pc" value="#{permisView.permis.numero}" />

                        <p:outputLabel for="points" value="Points :" />
                        <p:inputText id="points" />

                        <p:outputLabel for="categoie" value="Catégorie :" />
                        <p:selectManyCheckbox id="categoie">
                            <f:selectItem itemLabel="A1" itemValue="A1" />
                            <f:selectItem itemLabel="A" itemValue="A" />
                            <f:selectItem itemLabel="B" itemValue="B" />
                            <f:selectItem itemLabel="C" itemValue="C" />
                        </p:selectManyCheckbox>

                        <p:outputLabel for="categoie2" />
                        <p:selectManyCheckbox id="categoie2">
                            <f:selectItem itemLabel="D" itemValue="D" />
                            <f:selectItem itemLabel="EB" itemValue="EC" />
                            <f:selectItem itemLabel="EB" itemValue="EB" />
                            <f:selectItem itemLabel="ED" itemValue="ED" />
                        </p:selectManyCheckbox>

                    </h:panelGrid>

                    <p:commandButton value="Consulter" onclick="lirePC()">
                        <p:ajax update="interogerPC"/>

                    </p:commandButton>

                    <p:remoteCommand name="remplir" action="#{permisView.lirePermis}">
                    </p:remoteCommand>


                    <script type="text/javascript">

                        function lirePC() {

                            try {


                                document.embeds[0].getPublicDataPC();

                                remplir([ {
                                    name : 'PC1',
                                    value : document.embeds[0].PC1
                                }, {
                                    name : 'PC2',
                                    value : document.embeds[0].PC2
                                }, {
                                    name : 'PC3',
                                    value : document.embeds[0].PC3
                                } ])            

                            } catch (err) {
                                remplir([ {
                                    name : 'PC1',
                                    value : ''
                                }, {
                                    name : 'PC2',
                                    value : ''
                                }, {
                                    name : 'PC3',
                                    value : ''
                                } ])

                            }               

                        }


                    </script>
                </p:panel>
            </div>
        </h:form>
    </ui:define>

</ui:composition>

and here is lirePermis() function :

     public String lirePermis() {
        System.out.println("lire Permis");
        Map<String, String> params = FacesContext.getCurrentInstance()
                .getExternalContext().getRequestParameterMap();

        String PC1, PC2, PC3;

        permis = new PermisVO();
        PC1 = params.get("PC1");
        PC2 = params.get("PC2");
        PC3 = params.get("PC3");
        if (PC1 == "" || PC2 == "" || PC3 == "") {
            permis = new PermisVO();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erreur!", " Aucun permis détecté"));
                        System.out.println("pas de permis");
        }
        else {
            permis = permisService.getPermis(PC1, PC2, PC3);
            System.out.println(permis);         
        }
        return returnPath;
    }

Upvotes: 0

Views: 1083

Answers (1)

dlahlou
dlahlou

Reputation: 43

I found the problem : When the function lirePermis() ends the execution, I return "returnPath" which is the path of my xhtml page, so that causes the refresh of the whole page, that's why I was loosing my p:message,

The Solution is to NOT return anything (void)

public void lirePermis() {
    Map<String, String> params = FacesContext.getCurrentInstance()
            .getExternalContext().getRequestParameterMap();

    String PC1, PC2, PC3;

    permis = new PermisVO();
    PC1 = params.get("PC1");
    PC2 = params.get("PC2");
    PC3 = params.get("PC3");
    if (PC1 == "" || PC2 == "" || PC3 == "") {
        permis = new PermisVO();
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erreur!", " Aucun permis détecté"));

        //context.addMessage("notification", new FacesMessage(FacesMessage.SEVERITY_ERROR," Aucun permis détecté!!",null));
        System.out.println("pas de permis");
    }
    else {
        permis = permisService.getPermis(PC1, PC2, PC3);
        System.out.println(permis);         
    }

}

Then I found another problem, is that the message disappears immediately, and when I googled it, I found that I need to add a little "return false;" after calling my javascript function :

<p:commandButton value="Consulter" onclick="lirePC();  return false;">
    <p:ajax update="interogerPC" />
</p:commandButton>

Upvotes: 1

Related Questions