8

我有文件定位在里面WebContent/WEB-INF/resources/file/theFile.pdf,我可以知道如何下载将显示常规下载弹出对话框的文件或在网页中呈现它(只要最简单的方法都可以)当用户单击链接时页面?我正在使用 JSF2.0,目前正在使用h:outputLink下载 pdf 文件但没有运气,控制台输出显示此错误:

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>
4

2 回答 2

10

我有文件定位在里面WebContent/WEB-INF/resources/file/theFile.pdf

首先,/WEB-INF(和/META-INF)的内容是公开的(因为它们可能包含敏感信息,例如web.xml标签文件、模板等)。将文件放在公共网络内容之外 /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>

与具体问题无关library,您对属性的使用<h:graphicImage>毫无意义。另请参阅JSF 资源库的用途是什么以及应该如何使用它?

于 2012-10-03T12:40:07.620 回答
3

#{request.contextPath}用前缀试试

<h:outputLink value="#{request.contextPath}/resources/file/theFile.pdf">
    <h:graphicImage library="images" name="pdf.jpg" style="border:none"/>
</h:outputLink>
于 2012-10-03T06:59:37.420 回答