1

I've got a p:commandLink which call to bean method, which download file using Faces servlet. It works fine until I add a p:dialog in my page (commandLink doesn't inside it)

<p:commandLink value="#{file}"
    actionListener="#{bean.download(file)}"
    ajax="false"
    immediate="true"
    process="@this"/>

Ignoring that here's process="@this" commandLink try to process variable fileName in dialog

<p:dialog header="Edit" 
          widgetVar="editDialog" 
          modal="true">
        <div>
            <p:panelGrid>
                <p:row style="height: 50px;">
                    <p:column>
                        <p:inputText
                            value="#{bean.fileName}"
                            validator="#{fileName.validate}"
                            requiredMessage="file name is not filled"
                            validatorMessage="file with this name already exists"/>
                    </p:column>
                </p:row>
            </p:panelGrid>
        </div>
</p:dialog>

And i've got an exception:

javax.el.PropertyNotFoundException:value="#{bean.fileName}": Target Unreachable, 'null' returned null

I can't turn on ajax, because in this case file won't be downloaded

Backend:

public void download(InputStream is, String fileName) {
    ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
    context.setResponseContentType(context.getMimeType(fileName));
    context.setResponseHeader("Content-Disposition", "attachement;filename=" + fileName);
    try (OutputStream os = context.getResponseOutputStream()) {
        while(is.available() > 0) {
            os.write(is.read());
        }
    }
    catch(IOException ex) {
        LOG.error(ex);
    }
    finally {
        FacesContext.getCurrentInstance().responseComplete();
    }
}

How can I fix it? Without dialog everything works fine

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • Could help - https://stackoverflow.com/questions/18958729/pcommandbutton-action-doesnt-work-inside-pdialog – Ashish Mathew May 31 '18 at 07:58
  • _"I can't turn off ajax, because in this case file won't be downloaded"_ You **TRY** to turn of ajax by using ` ajax="fasle"` but have a typo in there – Kukeltje May 31 '18 at 09:32
  • My bad, I got confused, I can't turn on ajax – Simatsu Edgeworth May 31 '18 at 09:39
  • did you download file without the dialog ?!? – Yagami Light May 31 '18 at 10:20
  • Yes, and it’s downloads fine – Simatsu Edgeworth May 31 '18 at 10:21
  • Tried searching for _"javax.el.PropertyNotFoundException:value="#{bean.fileName}": Target Unreachable, 'null' returned null"_ in google (mayve remove the bean EL that is specific in your case? LOTS of info on this – Kukeltje May 31 '18 at 10:55
  • It's like if I get a NullPointer trying to call method from object, and will go to search what NullPointer is. I know what it is, I want to know why object is null. Same here, I know this error, I want to know why commandLink try to process dialog and how to avoid it – Simatsu Edgeworth May 31 '18 at 11:15
  • @AshishMathew it helped, thanks! – Simatsu Edgeworth May 31 '18 at 11:42
  • If the 'bean' is null, I cannot imagine that that is caused by not having a form around the dialog (inside is not needed since you don't have any 'appendTo'. There might have been other things in wrong in your page that were solved when you started playing with the form... – Kukeltje May 31 '18 at 12:19
  • it's because this variable initialize only some user's actions on page. Until that actions dialog won't appear. This commandLink, which not related with this dialog, affected dialog somehow. When I just added second form to dialog, commandLink start to process only main form. Dialog form and all inside it remain unchanged and initializing only after user's action, just as intended – Simatsu Edgeworth May 31 '18 at 12:35

0 Answers0