行动链接和下载链接在一起 [英] Action link and download link in one

查看:161
本文介绍了行动链接和下载链接在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示在JSF 2应用程序的页面上下载文件的链接。现在,问题是文件在创建时包含依赖于新数据库的数据。但是我想做的是创建它,并给用户一个链接,以便在一个操作中下载它。

I need to show a link to download a file on a page in a JSF 2 application. Now, the issue is that the file contains data that depends on fresh looks into the database at the time of creation. But what I want to do is create it AND give the user a link to download it in one action.

在两个操作中执行这个操作非常简单:有一个按钮来生成文件,并在链接生成后将其替换为下载该文件。

This would be very simple to do in two actions: have a button to generate the file, and replace it with a link to download the file once it's generated.

所以再一次的问题是可以在一个命令行链接中完成这个吗?

So the question again is can this be done in one click on a commandLink?

编辑,遵循下列BalusC的评论。以下是我要做的事情的具体细节,到目前为止已经做了。我在xhtml中有这个:

EDIT, following BalusC's comment below. Here's the specifics of what I am trying to do, and have done so far. I have this in the xhtml:

<h:panelGroup rendered="#{!bean.showLinkToExcelFile()}">
     <h:form>
         <td><h:commandButton value="Generate list of your Bids for download" action="#{bean.createBidsList()}"/></td>      
    </h:form>
</h:panelGroup>
<h:panelGroup rendered="#{bean.showLinkToExcelFile()}">
    <td><a href="#{bean.findBidsListFileName()}">Download Your Bids</a></td>
</h:panelGroup> 

这个工作。该按钮创建一个excel文件,将其保存到一个位置,并更新数据库中的文件名。然后链接提供文件。但这是用户的两步过程。我想要的只是一步。所以一个链接,例如:

This works. The button creates an excel file, saves it to a location, and updates the file name in the database. Then the link serves the file. But it's a two step process for the user. What I'd like it to be is just one step. So that one link, for example:

<a href="#{bean.findBidsListFileName()}">Download Your Bids</a>

或者,最有可能的是,jsf commandLink将在后端创建excel文件,将其保存到/资源/位置,并在用户的机器上无缝打开保存对话框。

or, most likely, a jsf commandLink will create the excel file on the back end, save it to the /resources/ location, and seamlessly open a "save" dialog on the user's machine.

推荐答案

您可以让JSF立即将报告写入 OutputStream 的HTTP响应,而不是本地磁盘文件系统的 OutputStream

You can just let JSF immediately write the report to the OutputStream of the HTTP response instead of to the OutputStream of the local disk file system.

这是一个基本的例子,假设您使用 Apache POI 创建Excel报告:

Here's a basic example, assuming that you're using Apache POI to create the Excel report:

public void createAndDownloadBidsReport() throws IOException {
    // Prepare Excel report to be downloaded.
    HSSFWorkbook bidsReport = createBidsReportSomehow();
    String filename = "bids.xls";

    // Prepare response to show a Save As dialogue with Excel report.
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    externalContext.setResponseContentType("application/vnd.ms-excel");
    externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

    // Write Excel report to response body.
    bidsReport.write(externalContext.getResponseOutputStream());

    // Inform JSF that response is completed and it thus doesn't have to navigate.
    facesContext.responseComplete();
}

这样,您只需单个命令链接(或按钮) 。

This way you can end up with just a single command link (or button).

<h:form>
    <h:commandLink value="Create and download bids report" action="#{bean.createAndDownloadBidsReport}" />
</h:form>

请注意,这不会将其保存到磁盘文件系统。如果您真的需要一份副本,那么您需要添加另一个 biddingReport.write()将写入所需磁盘文件系统的行位置。

Note that this doesn't save it to disk file system. If you really need to have a copy then you'd need to add another bidsReport.write() line which writes to the desired disk file system location.

这篇关于行动链接和下载链接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆