99maas
99maas

Reputation: 1267

How to show messages in p:dialog?

I need your help in showing an error message in the dialog. By clicking on the commandButton, no message is shown in the dialog.

Even though I tried to show the message in a dialog, but nothing is shown without any error.

So how can I produce messages in a dialog and not in the main form

Here is the JSF page code:

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

    <p:dialog id="c1" header="C1" widgetVar="c1">
        <p:message id="messagePDFSTAR"
                   for=":Requests:DownloadPDFSTAR"
                   showDetail="true" />
        <p:commandButton id="DownloadPDFSTAR"
                         value="Download"
                         ajax="false"
                         actionListener="#{hrd.PDFSTAR}"
                         update=":Requests:messagePDFSTAR" >

            <p:fileDownload value="#{hrd.fileSTAR}" />
        </p:commandButton>
    </p:dialog>
</h:form>

Here is the java bean code:

public void PDFSTAR() {

    try {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Ref is Null", "Ref is Null");
        RequestContext.getCurrentInstance().showMessageInDialog(message);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Fatal!", "System Error"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Upvotes: 1

Views: 10561

Answers (2)

alibttb
alibttb

Reputation: 545

To update components as p:growl or p:messages after a download, or even at the start just use a p:remoteCommand and the PrimeFaces.monitorDownload(start, stop); as in the example provided here PrimeFaces File Download example

here is how I'd do it:

<script type="text/javascript">
    function start() {
        PF('statusDialog').show();
        //maybe some other blah blah..
        updateMyMessages();
    }

    function stop() {
        PF('statusDialog').hide();
        //maybe some other blah blah..
        updateMyMessages();
    }
</script>

<div class="card">
    <p:dialog modal="true" widgetVar="statusDialog" header="Status" draggable="false" closable="false"
              resizable="false">
              <i class="pi pi-spinner pi-spin" style="font-size:3rem"></i>
    </p:dialog>

    <h:form>
        <p:messages id="messagesPDFSTAR"/>
        <p:remoteCommand name="updateMyMessages" update="messagesPDFSTAR" action="#{hrd.PDFSTAR}"/>
        <p:commandButton value="Download" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop);"
                         icon="pi pi-arrow-down" styleClass="p-mr-2">
            <p:fileDownload value="#{fileDownloadView.file}"/>
        </p:commandButton>
</div>

Please note that I called the remote command twice so you get two updates to your messages, also this will execute your action #{hrd.PDFSTAR} twice, once on download start and the other on download finish, so modify as you see fit.

You can always upgrade to PrimeFaces 10+ and use ajax="true" (the default) on the download command button.

Upvotes: 0

Adam M. Gamboa G.
Adam M. Gamboa G.

Reputation: 408

It's because you have ajax=false at the commandButton, so, the attribute update::Requests:messagePDFSTAR isn't going to work. You can't combine ajax=false and update="...", update attribute is only for ajax actions.

I understand you need the ajax=false because the p:fileDownload works with that, but maybe you can try another way to display the message you want.

I deal with that situation once and use a workaround, in p:dialog you can use a p:messages and autoUpdate=true.

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

    <p:dialog id="c1" header="C1" widgetVar="c1">
        <p:messages id="messagesPDFSTAR"
                    autoUpdate="true"
                   showDetail="true" />
        <p:commandButton id="DownloadPDFSTAR"
                         value="Download"
                         ajax="false"
                         actionListener="#{hrd.PDFSTAR}" >

            <p:fileDownload value="#{hrd.fileSTAR}" />
        </p:commandButton>
    </p:dialog>
</h:form>

I hope that will help you.

Edit:

RequestContext.getCurrentInstance().showMessageInDialog(message);

It shows the message in a new dialog, but i think it's not what you want. You want to show the message in the same dialog of the commandButton and the fileDownLoad.

Ajax and update not working correctly

Upvotes: 0

Related Questions