huahsin68
huahsin68

Reputation: 6989

How to make a download file in JSF 2.0?

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>

Upvotes: 8

Views: 5452

Answers (2)

Daniel
Daniel

Reputation: 37061

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>

Upvotes: 3

BalusC
BalusC

Reputation: 1109635

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?

Upvotes: 10

Related Questions