JSF 1.2 - 导出到Excel并重命名文件

时间:2013-09-16 11:11:58

标签: jsf jsf-1.2

在遗留代码中,excel导出是通过渲染xhtml(jsf)页面并将contentType更改为“application/vnd.ms-excel”来完成的,浏览器会自动下载该文件,并且可以使用excel打开此文件没有任何问题。

我想要更改的是文件的扩展名。下载文件时,它具有'xhtml'扩展名。我想使用适当的excel扩展名('xls')。

下载的文件名为summaryTransactionReports.xhtml。我想成为summaryTransactionReports.xls

如何以最小的影响实现这一目标?

以下是代码:

summaryTransactionReports.xhtml

<h:commandButton action="#{TransactionReports.createTransactionSummaryReportAction}"
                     value="#{msg.transactionReports_createReport}"
                     styleClass="form-button text-form-button"/>

TransactionReportsMBean.java

public String createTransactionSummaryReportAction()
{
    [...]
    Lots of thing here!!!
    [...]

    //Page we go to depends on the report type requested
    String nextPage;
    switch (reportType)
    {
        case REPORT_TYPE_HTML:
            nextPage = OutcomeConstants.VIEW_TRANSACTION_SUMMARY_REPORT;
            break;

        case REPORT_TYPE_EXCEL:
            nextPage = "viewExcelSummaryTransactionReport";
            break;

        case REPORT_TYPE_PRINTER_FRIENDLY:
            nextPage = OutcomeConstants.VIEW_TRANSACTION_SUMMARY_REPORT_PRINTER_FRIENDLY;
            break;

        default:
            nextPage = OutcomeConstants.VIEW_TRANSACTION_SUMMARY_REPORT;
            break;
    }

    return nextPage;
}

viewExcelSummaryTransactionReports.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ac="http://aconitesolutions.com/jsf-custom">
    <body>

        <ui:composition template="excelMainPage.xhtml">
            <ui:define name="title">#{msg.transactionReportsSummary_Title}</ui:define>

            <ui:define name="productName">Transaction Enabler</ui:define>
            <ui:define name="content"><ui:include src="summaryTransactionReportsContent.xhtml" /></ui:define>
            <ui:define name="header">This is the header stuff</ui:define>

        </ui:composition>

    </body>
</html>

excelMainPage.xhtml

<f:view contentType="application/vnd.ms-excel"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <head>
        <title>
            <ui:insert name="title">Default title</ui:insert>
        </title>
    </head>
    <body>

    <f:loadBundle basename="TRxEMessageResource" var="msg"/>

    <!-- div for displaying tooltips ; manipulated by javascript  -->
    <div id="tooltip" style="position:absolute;visibility:hidden"></div>


    <table style="width: 100%">
        <tr>
            <td>
                <div id="header">
                    <h2>#{msg.productName}</h2>
                </div>
            </td>
        </tr>

        <tr>
            <td style="vertical-align: top; width: 100%;
                border-style: solid;
                border-bottom-width: 2px;
                border-left-width: 0px;
                border-right-width: 0px;
                border-top-width: 0px;
                border-color: #A9A9A9;
                margin-bottom: 5%">
                <div id="titleText">
                    <h2>
                        <ui:insert name="title"/>
                    </h2>

                    <ui:insert name="content">
                        <div>Content goes here</div>
                    </ui:insert>
                </div>
            </td>
        </tr>
    </table>
    </body>

</f:view>

1 个答案:

答案 0 :(得分:1)

这可以被识别为MSIE特定行为。该浏览器确实不使用Content-Disposition标题中的文件名作为另存为文件名。相反,它使用来自请求URI的最后一个路径中显示的文件名,在这种情况下是提交的<form action>的URL。

最好的办法是让JSF将重定向发送到servlet,然后servlet返回报告。然后,您可以按照MSIE预期的方式在URL中直接指定文件名。

ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(ec.getRequestContextPath() + "/reports/filename.xls");

servlet看起来像这样:

@WebServlet("/reports/*")
public class ReportServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String filename = request.getPathInfo().substring(1);
        // ...
    }

}

生成报告所需的其他参数可以作为请求参数在重定向URL和/或通过会话传递。