0

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

sebi
  • 1,791
  • 3
  • 25
  • 43
  • Could you post the code of the method you are calling `pvtRunner.pvtLog`? Are you sure that the value changes and there is no update or could it be that the value does not change and there is an update? – user1983983 Jul 17 '13 at 12:46
  • I'm debugging - I only hit the breackpoint during the first call. – sebi Jul 17 '13 at 12:50
  • that dialog needs to be inside a `` if it's going to be able to send any requests to the server during the request lifecycle. Also move the processing you're doing in `getPvtLog` somewhere else, maybe onclick of the ``. Otherwise, you're setting yourself up for some longterm bugs in your code – kolossus Jul 17 '13 at 21:54

2 Answers2

1

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>
Community
  • 1
  • 1
Andy
  • 5,900
  • 2
  • 20
  • 29
1

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:

Community
  • 1
  • 1
kolossus
  • 20,559
  • 3
  • 52
  • 104
  • I tried your answer. It doesn't work. The trouble is with `onclick` because it's executed before an ajax request. It doesn't matter if `` is inside a `` or not. I can update my answer so you can try for yourself if you'd like. Maybe I'm not seeing everything you're doing but all I did was use his code and added the form like you suggested. I even have a silly `PvtRunner` class. Maybe I'm missing something ? – Andy Jul 17 '13 at 23:00
  • Even tried mixing your answer with your comment by adding the form processing inside the `onclick` and it still doesn't work. The value never gets updated. – Andy Jul 17 '13 at 23:28
  • @Andy - I didn't literally mean the `onclick` event. You can use the `action` attribute of the `` to initialize the data and then use the `oncomplete` event of the button to call `dlg.show()`. I'll update my answer with a snippet – kolossus Jul 18 '13 at 02:16
  • Oh, it's fine you don't have to. We're doing the (almost) same thing then. But also, you don't need the content of `` to be in ``. It only contains `outputText` and it's not sending data to the server. It will work without it. – Andy Jul 18 '13 at 02:42
  • @Andy - `getPvtLog()` will not be called if it's not in a `` – kolossus Jul 18 '13 at 03:12
  • Then why those the code above work (the **sample**)? By the way it doesn't matter if you do `update=":logDialogContnet"` or `update=":logDialog"`, it still works. Do you want me to post the bean again ? You can try it yourself. I'm not trying to argue by the way kolossus, I'm only trying to understand. If it seems like I am, sorry. – Andy Jul 18 '13 at 03:18
  • I'm even looking at the developer's guide for ``, they are not using ``. It doesn't seem like it's a requirement. From what I understand, it depends on what you need to do. In his case he's only displaying a value. He doesn't need it to be wrapped in a form. – Andy Jul 18 '13 at 03:59