sebi
sebi

Reputation: 1841

Dialog content does not get updated

I have searched this for hours, and I've found some stackoverflow questions related to it, but could not find a solution. I have a primefaces dialog that displays the content of a log file. The dialog is shown when pressing a button. My problem is that once the dialog is displayed, the content is never updated. That is, the #{pvtRunner.pvtLog} method is never called. How can I make my dialog call this method every time it is displayed, not just for the first time?

<h:head>
</h:head>

<h:body>
    <p:dialog id="logDialog" header="PVT Log" widgetVar="dlg" height="400" width="600" dynamic="true">
        <h:outputText id="logDialogContnet" value="#{pvtRunner.pvtLog}" escape="false"/>
    </p:dialog>

    <h:form>
        <p:panelGrid id="mainPanelGrid" columns="1" styleClass="panelGridBorder panelGridCenter">
            ....
            <p:commandButton id="showLog" value="Show Log" onclick="dlg.show()" type="button" update=":logDialogContnet"/>
        </p:panelGrid>
    </h:form>
</h:body>

This is the java method that should update the dialog content:

public String getPvtLog() {
        String result = "";
        try {
            File logFile = new File(instanceRoot + "\\config\\ProcessVerification.log");
            InputStream fis = new FileInputStream(logFile);
            result = readStream(fis);
        } catch (IOException e) {
            log.severe(e.getMessage());
        }
        return result;
}

Thanks

Upvotes: 0

Views: 141

Answers (2)

Andy
Andy

Reputation: 6598

First things first, stop doing your business logic in your getter. You should simply be returning the property. Below is one approach you could use. Make sure you check the link.

Modify your getPvtLog()

public String getPvtLog() {
    return result;
}

Then define a method that will be used for your actionListener in <p:commandButton>to update result. Example:

public void click() {    
    try {
        File logFile = new File(instanceRoot + "\\config\\ProcessVerification.log");
        InputStream fis = new FileInputStream(logFile);
        result = readStream(fis);
    } catch (IOException e) {
        log.severe(e.getMessage());
    }
    return result;
}

Change the method name as you see fit. This is just a quick example.

Now remove type="button" and instead of onclick change it to oncomplete. onclick is executed before an ajax request, you want <p:dialog> to pop up after the ajax request. I believe this is your main issue by the way. Also add an actionListener in your <p:commandButton>.

<p:commandButton id="showLog" actionListener="#{pvtRunner.click()}" value="Show Log" oncomplete="dlg.show()" update=":logDialogContnet"/>

Sample

<p:dialog id="logDialog" header="PVT Log" widgetVar="dlg" height="400" width="600" dynamic="true">
    <h:outputText id="logDialogContnet" value="#{pvtRunner.pvtLog}" escape="false"/>
</p:dialog>
<h:form>
    <p:panelGrid id="mainPanelGrid" columns="1" styleClass="panelGridBorder panelGridCenter">
        <p:commandButton id="showLog" actionListener="#{pvtRunner.click()}" value="Show Log" oncomplete="dlg.show()" update=":logDialogContnet"/>
    </p:panelGrid>
</h:form>

Upvotes: 1

kolossus
kolossus

Reputation: 20691

The dialog ought to have it's own <h:form/> if it's to be able to successfully call getPvtLog(). Per JSF rules, only components wrapped in a form will be able to send traffic to the server. So you should have:

<p:dialog id="logDialog" header="PVT Log" widgetVar="dlg" height="400" width="600" dynamic="true">
   <h:form>
    <h:outputText id="logDialogContnet" value="#{pvtRunner.pvtLog}" escape="false"/>
   </h:form>
</p:dialog>

On an unrelated note, you should move all that logic in your getPvtLog() outside of that method.

See why:

Upvotes: 1

Related Questions