8

I have file locate inside WebContent/WEB-INF/resources/file/theFile.pdf, may I know how can I download the file that will show a regular download popup dialog or render it in the webpage (either one will do as long as the simplest way to go) when user click on the link of a page? I am using JSF2.0, and currently using h:outputLink to download the pdf file but no luck, the console output show me this error:

File not found: /pages/resources/file/theFile.pdf

How can tell JSF to remove the /pages and begin with the /resources as my file was sit inside resources folder.

This is the download code:

<h:outputLink value="resources/file/theFile.pdf">
  <h:graphicImage library="images" name="pdf.jpg" style="border:none"/>
</h:outputLink>
huahsin68
  • 6,819
  • 20
  • 79
  • 113

2 Answers2

10

I have file locate inside WebContent/WEB-INF/resources/file/theFile.pdf

First of all, the content of /WEB-INF (and /META-INF) is not publicly available (as they may contain sensitive information such as web.xml, tag files, templates, etc). Put the file in public webcontent outside /WEB-INF.

Assuming that it's now located in WebContent/resources/file/theFile.pdf, then you can link to it as follows:

<h:outputLink value="#{request.contextPath}/resources/file/theFile.pdf">
    <h:graphicImage library="images" name="pdf.jpg" style="border:none"/>
</h:outputLink>

Unrelated to the concrete problem, your usage of library attribute on <h:graphicImage> makes no sense. See also What is the JSF resource library for and how should it be used?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
3

Try with #{request.contextPath} prefix

<h:outputLink value="#{request.contextPath}/resources/file/theFile.pdf">
    <h:graphicImage library="images" name="pdf.jpg" style="border:none"/>
</h:outputLink>
Daniel
  • 36,833
  • 10
  • 119
  • 200