Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a download file in JSF 2.0?

Tags:

jsf-2

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>
like image 822
huahsin68 Avatar asked Oct 03 '12 06:10

huahsin68


2 Answers

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?

like image 57
BalusC Avatar answered Jan 24 '23 07:01

BalusC


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>
like image 33
Daniel Avatar answered Jan 24 '23 06:01

Daniel