2011-05-12 55 views
1

基本上我们有一个JSF应用程序,它动态生成一个链接到一个提供PDF文件的servlet。我需要将PDF的路径传递给servlet。我不知道如何将数据传递给servlet。将数据传递给servlet

在查看我们:

<d:protocolSection value="#{detailBacker.studyData}" id="protocol" /> 

在控制器中,我们有

public string getFile() { 
    ....... 
    // some variable here that holds the folder and file name 
    result += "<a href=\"/catalog/catalog/WelcomeServlet\" target=\"_blank\">" + name + "</a> 
    ....... 
} 

基本上,我需要以某种方式发送保存的文件夹和文件名的WelcomeServlet的变量,以便WelcomeServlet可以使用它。

回答

2

将它作为请求参数或pathinfo传递给通常的Servlet方式。

下面是一个例子假设PATHINFO优先和#{bean.pdfpath}回报类似filename.pdf

<h:outputLink value="pdf/#{bean.pdfpath}">Download pdf</h:outputLink> 

在映射在/pdf/*的URL模式的servlet可以为doGet()方法如下得到它:

String pdfpath = request.getPathInfo(); 
// ... 

作为一个完全不同的选择,你也可以让JSF在命令链接/命令按钮操作方法中将PDF写入响应。

+0

看到我对Jigar的回答评论。这仍然有效吗? – Catfish

+0

我不明白为什么你会在控制器中生成视图。只要在视图中完成这项工作。如果你确实*坚持,你可以使用''来显示它。但它很臭。 – BalusC

+0

我已更新我的问题,并提供更多信息。 – Catfish

0

保持位置固定生成/创建的PDF文件,并只传递文件的名称,如

/pdfServlet?fileName=#{someBean.someFileName} 

在servlet的doGet()检索文件名和提供服务的文件。

String fileName = request.getParameter("fileName"); 
+0

Note that this way causes MSIE to save the file as 'pdfServlet' instead of the specified filename. It namely ignores the filename in the content disposition header and takes the last part of the path. – BalusC

+0

Now can I actually put that jsf code in a dynamically generated anchor tag? For example I have a controller that is generating the tag this way 'result += "" + name + "' – Catfish

+0

@BalusC如果我们在servlet中添加头文件?或者只是将用户重定向到文件? –