0

Is it possible to somehow handle error on loading a PDF?

<p:media value="/resources/media/myDoc.pdf" width="100%" height="800px" zoom="100" player="pdf" cache="false"<>/p:media>

Let's say the PDF document it's not there and won't find it, If that happens my web app crash. Possible to handle it? So I show an error message instead in growl for example?

Thanks

  • I use something based on https://stackoverflow.com/questions/27526753/exception-handling-in-jsf-ajax-requests mixed with https://stackoverflow.com/questions/42247737/add-global-message-when-field-validation-fails – Jasper de Vries Aug 17 '22 at 15:49

1 Answers1

0

You can check the @Jasper de Vries comment, otherwise you can verify if the file is present using a bean method like this:

MyBean.java

public String findFileURL() {
    String fileName = "myDoc.pdf";
    String relativeWebPath = "/resources/media/" + fileName;
    FacesContext facesContext = FacesContext.getCurrentInstance();
    String absoluteDiskPath = ((ServletContext) facesContext.getExternalContext().getContext())
            .getRealPath(relativeWebPath);
    File file = new File(absoluteDiskPath);
    if (file.isFile()) {
        return relativeWebPath;
    } else {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "File: " + fileName + " not found.", "");
        facesContext.addMessage(null, message);
        return "";
    }
}

MyPage.xhtml

<p:media value="#{myBean.findFileURL()}" width="100%" height="800px" zoom="100" player="pdf" cache="false"/>
stefan-dan
  • 586
  • 5
  • 19