如何在primefaces中完成上传后更新p:dataTable

时间:2017-08-28 20:43:49

标签: jsf primefaces jsf-2

我正在使用带有主要面孔的Web应用程序6.1

我有一个 p:fileUpload ,其中包含我用来上传某些图片的选项multiple =“true”。 完成上次上传后,我使用 p:remoteCommand 来更新包​​含刚刚上传的所有图片的表格。

在这个表中我有一些字段,除了布尔字段外,一切都很好。即使在相关控制器中始终为真,它们也总是假的。

这里有一些最小的代码

xhtml页面的重要部分

<p:fileUpload fileUploadListener="#{provaController.handleFileUploadOnAddAdvertising}"
                          widgetVar="advImageUploaderVar"
                          auto="true"
                          mode="advanced" 
                          multiple="true"
                          allowTypes="/(\.|\/)(jpe?g)$/"
                          oncomplete="handleMultiFileUploadRequest(PF('advImageUploaderVar'), afterAllUploadsCompleted);"

            />
            <p:remoteCommand name="afterAllUploadsCompleted" update="dataTableUploadedImages" />
            <p:dataTable id="dataTableUploadedImages" var="car" value="#{provaController.uploadedImagesOnAddAdvertisingAsList}">                                               
                <p:column headerText="booleanField" width="180">
                    <div align="center">
                        <p:selectBooleanButton value="#{car.booleanField}" onLabel="Yes" offLabel="No" />
                    </div>
                </p:column>
                <p:column headerText="booleanObjectField" width="180">
                    <div align="center">
                        <p:selectBooleanCheckbox value="#{car.booleanObjectField}" />
                    </div>
                </p:column>
                <p:column headerText="booleanAsStringField" width="180">
                    <div align="center">
                        <h:selectBooleanCheckbox value="#{car.booleanObjectField}" />
                    </div>
                </p:column>
                <p:column headerText="Date" width="180">
                    <div align="center">
                        <p:calendar value="#{car.dateField}" />
                    </div>
                </p:column>
                <p:column headerText="Integer" width="180">
                    <div align="center">
                        <p:inputNumber value="#{car.integerField}" />
                    </div>
                </p:column>
                <p:column headerText="String" width="180">
                    <div align="center">
                        <h:outputText value="#{car.stringField}" />
                    </div>
                </p:column>
                <p:column headerText="#{msg['anteprima']}">
                    <div align="center">
                        <p:graphicImage value="#{provaController.imagesForAddEditAdvertising}" width="200">
                            <f:param name="imageId" value="#{car.stringField}" />
                            <f:param name="cvlTimeStamp" value="#{now}" />
                        </p:graphicImage>
                    </div>
                </p:column>
                <p:column headerText="#{msg['azioni']}" width="70">
                    <div align="center">
                        <p:commandButton icon="ui-icon-trash" title="#{msg['cancella']}" action="#{provaController.removeUploadedImageOnAddAdvertising(car.stringField)}" update="dataTableUploadedImages" />
                    </div>
                </p:column>
            </p:dataTable>

控制器的重要部分

List<ProvaBean> uploadedImagesOnAddAdvertisingAsList = new ArrayList<>();
    private Map<String, ProvaBean> uploadedImagesOnAddAdvertising = new HashMap<>();

    public void handleFileUploadOnAddAdvertising(FileUploadEvent event) throws IOException {

        ProvaBean pb = new ProvaBean();

        pb.setBooleanField(true);
        pb.setBooleanObjectField(Boolean.TRUE);
        pb.setBooleanAsStringField("true");
        pb.setDateField(new Date());
        pb.setIntegerField(ThreadLocalRandom.current().nextInt());
        pb.setStringField(randomAlphanumeric(10));
        pb.setByteArrayField(event.getFile().getContents());

        uploadedImagesOnAddAdvertisingAsList.add(pb);
        uploadedImagesOnAddAdvertising.put(pb.getStringField(), pb);
    }

    public void removeUploadedImageOnAddAdvertising(String imageId) {
        ProvaBean a = uploadedImagesOnAddAdvertising.get(imageId);
        if (a != null) {
            uploadedImagesOnAddAdvertising.remove(imageId);
            uploadedImagesOnAddAdvertisingAsList.remove(a);
        }
    }

    public StreamedContent getImagesForAddEditAdvertising() {
        return getImagesForAddEditAdvertising(uploadedImagesOnAddAdvertising);
    }

    public StreamedContent getImagesForAddEditAdvertising(Map<String, ProvaBean> uploadedImagesOnAddAdvertising) {
        StreamedContent res;

        if (FacesContext.getCurrentInstance().getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            res = manageRenderResponse();
        } else {
            String imageId = retrieveImageIdFromContext(FacesContext.getCurrentInstance());
            ProvaBean image = uploadedImagesOnAddAdvertising.get(imageId);
            if (image != null) {
                res = new ByteArrayContent(image.getByteArrayField(), "image/jpg", image.getStringField(), StandardCharsets.UTF_8.name());
            } else {
                res = manageRenderResponse();
            }
        }

        return res;
    }

    private StreamedContent manageRenderResponse() {
        final Instant instant = Instant.now();
        String s = String.valueOf(instant.getEpochSecond());
        String n = String.valueOf(instant.getNano());
        return new ByteArrayContent(new byte[0], "image/jpg", s + n, StandardCharsets.UTF_8.name());
    }

    private String retrieveImageIdFromContext(FacesContext context) {
        return Optional.ofNullable(context.getExternalContext().getRequestParameterMap().get("imageId")).orElse("");
    }

    private String randomAlphanumeric(int length) {

        return ThreadLocalRandom.current().ints(48, 123)
                .filter(i -> (i < 58) || (i > 64 && i < 91) || (i > 96))
                .limit(length)
                .collect(StringBuilder::new, (sb, i) -> sb.append((char) i), StringBuilder::append).toString();

    }

ProvaBean是一个简单的pojo,包含以下字段

    private Integer integerField;
    private Boolean booleanObjectField;
    private boolean booleanField;
    private String booleanAsStringField;
    private Date dateField; 
    private String stringField;
    private byte[] byteArrayField;

为了清楚起见,我提醒您注意包含基本知识的非常详细的original questionmy github repository,以便重现问题。

只需下载存储库并在TomEE或Glassfish中启动它。 在此之后,上传一个图像,在底部表中,即使控制器将该字段设置为true,您也会看到所有布尔字段值都为false。

为什么呢?难道我做错了什么?你能救我吗?

0 个答案:

没有答案