-2

我需要帮助在 JSF 页面中实现文件下载。到目前为止,我设法创建了这个:

在此处输入图像描述

    <?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"    
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <ui:insert name="header">           
            <ui:include src="header.xhtml"/>         
        </ui:insert>
    </h:head>
    <h:body>
        <h1><img src="resources/css/images/icon.png" alt="DX-57" /> Settings Center</h1>
        <!-- layer for black background of the buttons -->
        <div id="toolbar" style="margin: 0 auto; width:100%; height:30px; position:relative;  background-color:black">
            <!-- Include page Navigation -->
            <ui:insert name="Navigation">           
                <ui:include src="Navigation.xhtml"/>         
            </ui:insert>
        </div>  
        <div id="logodiv" style="position:relative; top:35px; left:0px;"> 
            <h:graphicImage alt="Glassfish"  style="position:relative"  value="resources/images/logo_glassfish_settings.png" />
        </div>
        <div id="main" style="margin: 0 auto; width:1190px; height:700px; position:absolute;  background-color:transparent; top:105px">

            <div id="mainpage" style="margin: 0 auto; width:1190px; height:500px; position:absolute;  background-color:transparent; top:80px">

                <div id="settingsdiv" style="width:350px; height:400px; position:absolute;  background-color:r; top:20px; left:1px">

                    <div id="settingsHashMap" style="width:1050px; height:400px; position:absolute;  background-color:r; top:20px; left:1px">

                        <h:form id="form" >
                            <p:growl id="growl" showDetail="true" sticky="true" />
                            <!-- The sortable data table -->
                            <h:dataTable onmouseover="addOnclickToDatatableRows();" id="dataTable" headerClass="columnHeader" value="#{GlassfishController.dataList}" binding="#{table}" var="item">
                                <!-- Check box -->
                                <h:column>
                                    <f:facet name="header">
                                        <h:selectBooleanCheckbox style="margin-left: 0px;" value="#{GlassfishController.mainSelectAll}" class="checkall" >
                                            <f:ajax listener="#{GlassfishController.selectAll}" render="@form" />
                                        </h:selectBooleanCheckbox>
                                    </f:facet>
                                    <h:selectBooleanCheckbox  onclick="highlight(this)" value="#{GlassfishController.selectedIds[item.filename]}" >
                                        <!-- if the user deselects one row deselect the main checkbox -->
                                        <f:ajax listener="#{GlassfishController.deselectMain}" render="@form" />
                                    </h:selectBooleanCheckbox>
                                    <!-- Click on table code -->  
                                    <h:outputLink id="lnkHidden" onclick="document.getElementById('form:btnHello').click(); return false;" style="text-decoration:none; color:white; background-color:black">
                                    </h:outputLink>
                                </h:column>
                                <!-- Row number -->
                                <h:column>
                                    <f:facet name="header">
                                        <h:outputText value="№" />                                    
                                    </f:facet>
                                    <h:outputText value="#{table.rowIndex + GlassfishController.firstRow + 1}" />
                                </h:column>                                
                                <h:column>
                                    <f:facet name="header">
                                        <h:commandLink value="File Name" actionListener="#{GlassfishController.sort}" style="text-decoration:none; color:white; background-color:black">
                                            <f:attribute name="sortField" value="filename" />
                                            <f:ajax render="@form" />
                                        </h:commandLink>
                                    </f:facet>
                                    <h:outputText value="#{item.filename}" />
                                </h:column>
                                <h:column>
                                    <f:facet name="header">
                                        <h:commandLink value="Size" actionListener="#{GlassfishController.sort}" style="text-decoration:none; color:white; background-color:black">
                                            <f:attribute name="sortField" value="size" />
                                            <f:ajax render="@form" />
                                        </h:commandLink>
                                    </f:facet>
                                    <h:outputText value="#{item.size} bytes" />
                                </h:column>

                            </h:dataTable>

                            <!-- The paging buttons -->
                            <h:commandButton value="first" action="#{GlassfishController.pageFirst}"
                                             disabled="#{GlassfishController.firstRow == 0}" >
                                <f:ajax render="@form" execute="@form"></f:ajax>
                            </h:commandButton>&nbsp;


                            <h:commandButton value="prev" action="#{GlassfishController.pagePrevious}"
                                             disabled="#{GlassfishController.firstRow == 0}" >
                                <f:ajax render="@form" execute="@form"></f:ajax>
                            </h:commandButton>&nbsp;

                            <h:commandButton value="next" action="#{GlassfishController.pageNext}"
                                             disabled="#{GlassfishController.firstRow + GlassfishController.rowsPerPage >= GlassfishController.totalRows}" >
                                <f:ajax render="@form" execute="@form"></f:ajax>
                            </h:commandButton>&nbsp;    

                            <h:commandButton value="last" action="#{GlassfishController.pageLast}"
                                             disabled="#{GlassfishController.firstRow + GlassfishController.rowsPerPage >= GlassfishController.totalRows}" >
                                <f:ajax render="@form" execute="@form"></f:ajax>
                            </h:commandButton>&nbsp;

                            <h:outputText value="Page #{GlassfishController.currentPage} / #{GlassfishController.totalPages}" />
                            <br />

                            <!-- The paging links -->
                            <ui:repeat value="#{GlassfishController.pages}" var="page">
                                <h:commandLink value="#{page}" actionListener="#{GlassfishController.page}"
                                               rendered="#{page != GlassfishController.currentPage}" style="text-decoration:none;color:white;">
                                    <f:ajax render="@form" execute="@form"></f:ajax>   
                                </h:commandLink>
                                <h:outputText value="#{page}" escape="false"
                                              rendered="#{page == GlassfishController.currentPage}" style="text-decoration:none;color:gray;"/>
                            </ui:repeat>
                            <br />

                            <!-- Set rows per page -->
                            <h:outputLabel for="rowsPerPage" value="Rows per page" />
                            <h:inputText id="rowsPerPage" value="#{GlassfishController.rowsPerPage}" size="3" maxlength="3" />
                            <h:commandButton value="Set" action="#{GlassfishController.pageFirst}" >
                                <f:ajax render="@form" execute="@form"></f:ajax>
                            </h:commandButton>&nbsp;
                            <h:message for="rowsPerPage" errorStyle="color: red;" />

                            <!-- hidden button -->
                            <h:commandButton id="deleterow" value="HiddenDelete" action="#{GlassfishController.deleteSelectedIDs}" style="display:none">
                                <f:ajax render="@form"></f:ajax>
                            </h:commandButton>

                            <!-- the delete button -->
                            <h:button value="Delete" onclick="deletedialog(this, 'Do you want to delete the selected rows?'); return false;" />

                            <script type="text/javascript" src="resources/js/tabs.js"></script>


                            <h:commandLink id="btnHello" value="Download Files"  
                                           action="#{GlassfishController.downloadFile}">  
                            </h:commandLink>
                        </h:form>                    
                    </div> 

                </div>                    
            </div>  
        </div>
    </h:body>
</html>


// Execute the download procedure
    public void executeDownload(String filename) throws IOException{
        int DEFAULT_BUFFER_SIZE = 10240;
        String filePath = "/opt/glassfish3/glassfish/domains/domain1/logs/" + filename;

        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        File file = new File(filePath);
        if (!file.exists())
        {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        response.reset();
        response.setBufferSize(DEFAULT_BUFFER_SIZE);
        response.setContentType("text");
        response.setHeader("Content-Length", String.valueOf(file.length()));
        response.setHeader("Content-Disposition", "attachment;filename=\""
                + file.getName() + "\"");
        BufferedInputStream input = null;
        BufferedOutputStream output = null;
        try
        {
            input = new BufferedInputStream(new FileInputStream(file),
                    DEFAULT_BUFFER_SIZE);
            output = new BufferedOutputStream(response.getOutputStream(),
                    DEFAULT_BUFFER_SIZE);
            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            int length;
            while ((length = input.read(buffer)) > 0)
            {
                output.write(buffer, 0, length);
            }
        }
        finally
        {
            input.close();
            output.close();
        }
        context.responseComplete();
    }

有一个我无法解决的问题,我需要帮助。当我单击下载按钮时,我可以成功下载日志文件。单击文件时,我想将文件下载到 JSF 表中。你能帮我实现它吗?或者你能给我一些建议吗?

最好的祝愿

4

1 回答 1

2

根据您的评论,您可以使用 commandLink/commandButton 在同一页面中调用另一个 commandLink/commandButton。请记住,这些组件的行为类似于普通的 HTML 组件。发布此行为的示例

<h:form id="frmTest">
    <h:commandLink value="I will call the commandButton below me"
        onclick="document.getElementById('frmTest:btnHello').click(); return false;" />
    <br />
    <h:commandButton id="btnHello" value="I'm the commandButton below the commandLink"
        action="#{bean.someAction}" />
</h:form>

编辑:根据您对问题的评论,您有几种方法可以将参数传递给您的 commandButton 操作。我将向您展示一个使用 JavaScript 的。逻辑很简单:

  • 使用<h:inputHidden>标签组件,其值将是您要下载的文件的名称。
  • 在发送onclick事件之前,用你需要的值填充这个隐藏的组件。
  • 让 HTML 请求继续其工作。
<h:form id="frmTest">
    <h:commandLink id="hidFileName" value="#{bean.fileToDownload}" />
    <h:commandLink value="I will call the commandButton below me"
        onclick="document.getElementById('frmTest:hidFileName').value='#{bean.fileName}'; document.getElementById('frmTest:btnHello').click(); return false;" />
    <br />
    <h:commandButton id="btnHello" value="I'm the commandButton below the commandLink"
        action="#{bean.someAction}" />
</h:form>
于 2012-08-27T03:24:03.907 回答