English 中文(简体)
未经Ajax
原标题:Error when submitting a form with PrimeFaces without using Ajax

我正在利用雅加达服务器面孔和JuanWeb项目12。 我正试图处理一份表格,其中含有4个选择“一个小岛”,相互更新(第一部分的选定内容更新了第二部分的内容,第二部分的选定内容更新了第三部分的内容等等)。 我使用了Ajax,以便改变其中任何一个选择的状况。 1Menu更新其余部分的数值 1Menu。

The submit button of the form processes the entire form, but I need it not to use Ajax to process the form because this button also downloads a report resulting from the values selected in the selectOneMenu.

如果我把Ajax = 代码对表格的提交年份表示正确,则该代码处理,但如果我把Ajax分解到纽州,即使所有选定的数值都选择了有效数值,但该档案没有下载。 1Menu, 他们犯了一个错误,表明价值无效,形式未完成。

我介绍了我使用的守则:

            #{reportsBean.update_Area()}
            <h:form id="form1">
                <p:growl id="messages" allowDismiss="true"/>
                <div class="row">
                    <div class="col col-sm-6">
                        <p:outputLabel for="areaInput"
                                       styleClass="block"> &Aacute;rea </p:outputLabel> <br/>
                        <p:selectOneMenu value="#{reportsBean.area}"
                                         id="areaInput"
                                         requiredMessage="&Aacute;rea: Es necesario seleccionar una Facultad"
                                         required="true" label="&Aacute;rea">
                            <f:selectItem itemLabel="(Seleccione un &Aacute;rea)" itemValue=""/>
                            <f:selectItems value="#{reportsBean.list_area}" var="a" itemValue="#{a}"
                                           itemLabel="#{a}"/>
                            <p:ajax update="carrera plan_estudio asignatura"/>
                        </p:selectOneMenu>
                    </div>
                    <div class="col col-sm-6">
                        <p:outputPanel id="carrera">
                            #{reportsBean.updtate_carrera()}
                            <p:outputLabel for="carreraInput"
                                           styleClass="block"> Carrera </p:outputLabel> <br/>
                            <p:selectOneMenu value="#{reportsBean.carrera}"
                                             id="carreraInput"
                                             requiredMessage="Carrera: Es necesario seleccionar una Carrera"
                                             required="true" label="Carrera">
                                <f:selectItem itemLabel="(Seleccione una Carrera)" itemValue=""/>
                                <f:selectItems value="#{reportsBean.list_carrera}" var="c" itemValue="#{c}"
                                               itemLabel="#{c}"/>
                                <p:ajax update="plan_estudio asignatura"/>
                            </p:selectOneMenu>
                        </p:outputPanel>
                    </div>
                </div>
                <br/>
                <div class="row">
                    <div class="col col-sm-6">
                        <p:outputPanel id="plan_estudio">
                            #{reportsBean.update_plan_estudio()}
                            <p:outputLabel for="planEstudioInput"
                                           styleClass="block"> Plan de Estudio </p:outputLabel> <br/>
                            <p:selectOneMenu id="planEstudioInput" value="#{reportsBean.plan_estudio}"
                                             requiredMessage="Plan de Estudio: Es necesario seleccionar un Plan de Estudio"
                                             required="true" label="Plan de Estudio">
                                <f:selectItem itemLabel="(Seleccione un Plan de Estudio)" itemValue=""/>
                                <f:selectItems value="#{reportsBean.list_plan_estudio}" var="pe" itemValue="#{pe}"
                                               itemLabel="#{pe}"/>
                                <p:ajax update="asignatura"/>
                            </p:selectOneMenu>
                        </p:outputPanel>
                    </div>
                    <div class="col col-sm-6">
                        <p:outputPanel id="asignatura">
                            <p:outputLabel for="asignaturaInput"
                                           styleClass="block"> Asignatura </p:outputLabel> <br/>
                            #{reportsBean.update_Asignaturas()}
                            <p:selectOneMenu value="#{reportsBean.asignatura_Ms13d}"
                                             id="asignaturaInput"
                                             requiredMessage="Asignatura: Es necesario seleccionar una Asignatura"
                                             required="true" label="Asignatura">
                                <f:selectItem itemLabel="(Seleccione una Asignatura)" itemValue=""/>
                                <f:selectItems value="#{reportsBean.list_asignatura}" var="pe" itemValue="#{pe}"
                                               itemLabel="#{pe}"/>
                            </p:selectOneMenu>
                        </p:outputPanel>
                    </div>
                </div>
                <br/>

                <div style="float: right">
                    <p:commandButton title="PDF" style="margin-right: 10px" icon="pi pi-file-pdf"
                                     styleClass="ui-button ui-button-secondary" ajax="false"
                                     actionListener="#{reportsBean.print_rs4b_tipo1_PDF()}">
                    </p:commandButton>
                    <p:commandButton title="DOC" style="margin-right: 10px" icon="pi pi-file-word"
                                     styleClass="ui-button ui-button-secondary" ajax="false"
                                     actionListener="#{reportsBean.print_rs4b_tipo1_DOC()}">
                    </p:commandButton>
                    <p:commandButton title="RTF" style="margin-right: 10px" icon="pi pi-file"
                                     styleClass="ui-button ui-button-secondary" ajax="false"
                                     actionListener="#{reportsBean.print_rs4b_tipo1_RTF()}">
                    </p:commandButton>
                </div>

                <input type="hidden"
                       name="${_csrf.parameterName}"
                       value="${_csrf.token}"/>
            </h:form>

这是一种曾经填满的表格的图像,产生了PDF纽顿,形式价值被删除,每个选择的错误信息被删除。 展示了1Menu。

“Mensaje

我希望你能够帮助我解决这一问题。 预 收

问题回答

由于Jasper de Vries在评论中作了答复。 在此,我给你一个完整的解决办法。

To resolve this situation, ajax can be used. The Download component of primefaces can be used. Using the Primefaces StreamedContent class, the stream of the generated report can be obtained and later sent to the view through p:filedownloader.

这一观点如下:

<h:form id="form1">
                <p:growl id="messages" allowDismiss="true"/>
                <div class="row">
                    <div class="col col-sm-3">
                        <p:outputLabel for="idSearch"
                                       styleClass="block"> Identificaci&oacute;n </p:outputLabel>
                        <p:inputMask
                                id="idSearch"
                                mask="99999999999"
                                requiredMessage="ERROR: Es necesario un valor para establecer la b&uacute;squeda"
                                validatorMessage="ERROR: Idemtificaci&oacute;n no v&aacute;lida"
                                placeholder="Identificaci&oacute;n" value="#{reportsBean.idEstudiante}"
                                required="true"
                        />
                    </div>
                    <div class="col-sm-5">
                        <br/>
                        <p:commandButton title="PDF" style="margin-right: 10px" icon="pi pi-file-pdf"
                                         styleClass="ui-button ui-button-secondary" ajax="true" update="form1"
                                         actionListener="#{reportsBean.printCertCalifCursoAc( PDF )}">
                            <p:fileDownload value="#{reportsBean.file}"/>
                        </p:commandButton>
                        <p:commandButton title="DOCX" style="margin-right: 10px" icon="pi pi-file-word"
                                         styleClass="ui-button ui-button-secondary" ajax="true" update="form1"
                                         actionListener="#{reportsBean.printCertCalifCursoAc( DOCX )}">
                            <p:fileDownload value="#{reportsBean.file}"/>
                        </p:commandButton>
                        <p:commandButton title="RTF" style="margin-right: 10px" icon="pi pi-file"
                                         styleClass="ui-button ui-button-secondary" ajax="true" update="form1"
                                         actionListener="#{reportsBean.printCertCalifCursoAc( RTF )}">
                            <p:fileDownload value="#{reportsBean.file}"/>
                        </p:commandButton>
                    </div>
                </div>
                <input type="hidden"
                       name="${_csrf.parameterName}"
                       value="${_csrf.token}"/>
            </h:form>

然后,在星号中,这些基地准备处理文件,界定了<条码>StreamedContent<> 代码>标的,并将报告数据送交报告处理员。

public void printCertCalifCursoAc(String type){
        Map<String, Object> params = new java.util.HashMap<String, Object>();
        params.put("id", idEstudiante);
        String fileName = "Certificado de Calificaciones de Curso Académico - " + idEstudiante + "." + type.toLowerCase();
        String jasperPath = URL + "ms19d.jasper";
        FileType fileType = FileType.valueOf(type);
        try {
            file = reportUtil.processReport(params, jasperPath, fileName, fileType);
        } catch (JRException | IOException e) {
            throw new RuntimeException(e);
        }
    }

最后,考虑到待处理的档案类型(PDF、RTF或DOCX)。 <代码>StreamContent被送至Bean,然后送交意见。

private StreamedContent getStreamedcontent(ByteArrayOutputStream outputStream, String fileName) throws IOException {
        InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
        SerializableSupplier<InputStream> supplier = () -> inputStream;
        StreamedContent streamedContent = DefaultStreamedContent.builder()
                .contentType("application/octet-stream")
                .name(fileName)
                .stream(supplier)
                .build();
        outputStream.flush();
        outputStream.close();
        inputStream.close();
        return streamedContent;
    }

    public StreamedContent processReport(Map<String, Object> params, String jasperpath, String fileName, FileType fileType) throws IOException, JRException {
        if (params == null) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR: Debe seleccionar o introducir un valor", "Error"));
        }
        String relativeWebPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath(jasperpath);
        File file = new File(relativeWebPath);
        Connection con = connection.getConnectionInstance("org.postgresql.Driver", "jdbc:postgresql://localhost:5432/SIGENU_EaD", "postgres", "postgres");
        String filename = file.getPath();
        JasperPrint print = JasperFillManager.fillReport(filename, params, con);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        switch (fileType) {
            case PDF -> JasperExportManager.exportReportToPdfStream(print, outputStream);
            case RTF -> {
                JRRtfExporter exporter = new JRRtfExporter();
                exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
                exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
                exporter.exportReport();
            }
            case DOCX -> {
                JRDocxExporter exporter = new JRDocxExporter();
                exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
                exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
                exporter.exportReport();
            }
        }
        return getStreamedcontent(outputStream, fileName);
    }




相关问题
JSF a4j:support with h:selectManyCheckbox

I m having trouble with a JSF selectManyCheckbox and A4J support. The purpose is to run some action when a checkbox is selected. This works perfectly in Firefox. Yet, when testing in any IE (ie6 / ie7 ...

Mojarra for JSF Encoding

Can anyone teach me how to use mojarra to encode my JSF files. I downloaded mojarra and expected some kind of jar but what i had downloaded was a folder of files i don t know what to do with

如何拦截要求终止?

在共同基金中,如果用户要求终止,就需要采取一些行动。 我需要某种拦截器,但我不知道如何这样做。 我需要帮助。 增 编

ICEFaces inputFile getting the file content without upload

Is there any way of just getting the content of the browsed file without any upload/file transfer operations? I currently use ICEFaces inputFile component but I do not need the default uploading ...

Weird behaviour of h:commandLink action (MethodExpression)

I have two JSPs where I am displaying some info from database in a h:dataTable. One of them is showing all the info, and one of them user specifically. I have showXML.jsp that shows the "XML" column ...

How to correctly use ResultSet with h:dataTable

The problem is, that after displaying the ResultSet with <h:dataTable>, the connection is left open. If I close it, it closes the ResultSet too. I m thinking about copying the ResultSet data ...

热门标签