简体   繁体   English

JSF 2.2和Primefaces 4.0:文件上传后刷新图像

[英]JSF 2.2 and Primefaces 4.0: Refreshing image after file upload

In my application I use JSF2.2 and Primefaces 4.0. 在我的应用程序中,我使用JSF2.2和Primefaces 4.0。 I have a functionality that uploads the new image and needs to update the existing one. 我具有上载新图像并需要更新现有图像的功能。 Sometimes after new image is uploaded, the old one might still be shown, it may happen because of cache or re-rendering problems. 有时,在上载新图像后,仍然可能会显示旧图像,这可能是由于缓存或重新渲染问题而发生的。 I can see that this especially usual happens with high resolution images like 2500x1600. 我可以看到,这种特别常见的情况发生在2500x1600之类的高分辨率图像上。 This can be reproduced in IE, Chrome and Firefox. 可以在IE,Chrome和Firefox中复制。 Here are my Primefaces components: 这是我的Primefaces组件:

<h:panelGroup id="mr-img-panel">
    <h:outputLink id="mr-image-link" target="_blank" process="@this"
                  value="#{myBean.image}"
                  rendered="#{myBean.image ne null}">
        <p:graphicImage id="mr-image" value="#{myBean.image}"
                        cache="false"
                        title="Title" styleClass="image-preview" />
    </h:outputLink>
</h:panelGroup>

<h:panelGroup styleClass="upload-view">
    <p:fileUpload id="mr-upload"
                  fileUploadListener="#{myBean.handleFileUpload}"
                  mode="advanced"
                  auto="true"
                  label="Upload new image"
                  dragDropSupport="true"
                  invalidFileMessage="#{msg['msg.invalid.filetype']}"
                  messageTemplate=" "
                  invalidSizeMessage="#{msg['msg.invalid.filesize']}"
                  update="mr-upload-messages, mr-img-panel"
                  sizeLimit="2097152"
                  allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
                  styleClass="upload-view-button"
                  oncomplete="setTimeout(function() { $('[id$=mr-upload-messages]').hide(1000); }, 2000);">
        <f:attribute name="someId" value="#{some.id}" />
    </p:fileUpload>

    <p:messages id="mr-upload-messages" showDetail="true" showSummary="false" closable="true"/>

At first, I thought that this can happen because of cache problems, so I have written a cache control listener: 起初,我认为这可能是由于缓存问题而发生的,因此我编写了一个缓存控件侦听器:

public class CacheControlPhaseListener implements PhaseListener {


    @Override
    public void afterPhase(final PhaseEvent event) {

    }

    @Override
    public void beforePhase(final PhaseEvent event) {
        final FacesContext facesContext = event.getFacesContext();
        final HttpServletResponse response = (HttpServletResponse) facesContext
                .getExternalContext().getResponse();
        response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
        response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
        // some date in the past
        response.setDateHeader("Expires", 0); // Proxies.
    }

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.RENDER_RESPONSE;
    }
}

But that didn't help. 但这没有帮助。 I have tried few different techniques and with some of them I had problem, described in HERE . 我曾尝试一些不同的技术,并与其中一些我有问题,说明这里 And as was told in this SOLUTION I wrote this FileUploadRenderer: 正如在此解决方案中所告诉的那样,我编写了这个FileUploadRenderer:

public class CustomFileUploadRenderer extends FileUploadRenderer {

    @Override
    public void decode(final FacesContext context, final UIComponent component) {
        if (context.getExternalContext().getRequestContentType().toLowerCase().startsWith("multipart/")) {
            super.decode(context, component);
        }
    }
}

And here is what I added to faces-config.xml for these classes: 这是我为这些类添加到faces-config.xml中的内容:

<lifecycle>
    <phase-listener id="nocache">com.example.CacheControlPhaseListener</phase-listener>
</lifecycle>

<render-kit>
    <renderer>
        <component-family>org.primefaces.component</component-family>
        <renderer-type>org.primefaces.component.FileUploadRenderer</renderer-type>
        <renderer-class>com.example.CustomFileUploadRenderer</renderer-class>
    </renderer>
</render-kit>

But the problem still exists. 但是问题仍然存在。 After uploading the new image, the old one is not refreshed by AJAX, it can only be seen after refreshing the page. 上载新图像后,旧图像不会被AJAX刷新,只有刷新页面后才能看到。

万一有人需要,似乎更新到Primefaces 5.0为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM