2012-10-03 23 views
8

我有文件找到WebContent/WEB-INF/resources/file/theFile.pdf,我可以知道如何下載這個文件,它會顯示一個常規的下載彈出對話框或在網頁中顯示它(只要用戶點擊最簡單的方法就可以做到這一點)一個頁面的鏈接?我使用JSF2.0,和目前使用h:outputLink下載pdf文件,但沒有運氣,控制檯輸出告訴我這個錯誤:如何在JSF 2.0中創建下載文件?

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

怎麼能告訴JSF刪除/pages並與/resources作爲開始我的文件位於資源文件夾內。

這是下載代碼:

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

回答

10

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

首先的/WEB-INF(和/META-INF)含量爲公開可用(因爲它們可能包含敏感信息,如web.xml,標記文件,模板等)。將文件放在公共webcontent 之外/WEB-INF

假設它現在位於WebContent/resources/file/theFile.pdf,那麼你就可以鏈接到它,如下所示:

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

無關到具體的問題,你對<h:graphicImage>library屬性的使用是沒有意義的。另見What is the JSF resource library for and how should it be used?

+0

一個非常全面的答案。 – huahsin68

+0

不客氣。 – BalusC

3

嘗試用#{request.contextPath}前綴

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