刷新后PrimeFaces fileUploader不可见

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了刷新后PrimeFaces fileUploader不可见相关的知识,希望对你有一定的参考价值。

我有一个fileUploader,在部分网站刷新后会消失

<?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">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                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:c="http://java.sun.com/jsp/jstl/core"
                xmlns:forgeview="http://jboss.org/forge/view"
                xmlns:rich="http://richfaces.org/rich"
                xmlns:a4j="http://richfaces.org/a4j" xmlns:p="http://primefaces.org/ui"
                template="/resources/scaffold/pageTemplate.xhtml">

    <f:metadata>
        <f:viewParam name="id" value="#{gameBean.id}"/>
        <f:event type="preRenderView" listener="#{gameBean.retrieve}"/>
    </f:metadata>

    <ui:param name="pageTitle" value="Create Game"/>

    <ui:define name="header">
        Game
    </ui:define>

    <ui:define name="subheader">
        <c:choose>
            <c:when test="#{!empty gameBean.id}">
                Bearbeiten eines Spiels
            </c:when>
            <c:otherwise>
                Hinzufügen eines neuen Spiels
            </c:otherwise>
        </c:choose>
    </ui:define>

    <ui:define name="footer"/>

    <ui:define name="main">

        <h:form id="create" enctype="multipart/form-data">
            <h:messages globalOnly="true" styleClass="error"/>

            <h:panelGrid id="grid" columnClasses="label,component,required" columns="3">
                <h:outputLabel for="gameBeanGameName" value="Name:"/>
                <h:panelGroup>
                    <h:inputText id="gameBeanGameName" value="#{gameBean.game.name}"/>
                    <h:message for="gameBeanGameName" styleClass="error"/>
                </h:panelGroup>
                <h:outputText/>
                <h:outputLabel for="gameBeanGameType" value="Typ: "/>
                <h:panelGroup>
                    <p:selectOneMenu id="gameBeanGameType" value="#{gameBean.game.type}">
                        <f:selectItems value="#{gameBean.types}"/>
                    </p:selectOneMenu>

                </h:panelGroup>
                <h:outputText/>
                <h:outputLabel rendered="#{fileUploadBean.size > 0}" for="cover"/>
                <a4j:mediaOutput rendered="#{fileUploadBean.size > 0}" element="img" mimeType="#{file.mime}" id="cover"
                                 createContent="#{fileUploadBean.paint}"
                                 style="width:100px; height:100px;" cacheable="false">
                </a4j:mediaOutput>
                <h:outputText />
                <h:outputLabel for="gameBeanGameCover"/>
                <h:panelGroup>
                    <h:form enctype="multipart/form-data">
                    <p:fileUpload auto="true"  id="gameBeanGameCover" update="@all" fileUploadListener="#{fileUploadBean.listener}"
                                  mode="advanced" allowTypes="/(.|/)(gif|jpe?g|png)$"/>
                    </h:form>
                </h:panelGroup>
                <h:outputText />
                <h:outputLabel for="gameBeanGameTrailer" value="TrailerURL:"/>
                <h:panelGroup>
                    <h:inputText id="gameBeanGameTrailer" value="#{gameBean.game.trailer}"/>
                    <h:message for="gameBeanGameTrailer" styleClass="error"/>
                </h:panelGroup>
                <h:outputText/>

                <h:outputLabel for="gameBeanGameDescription" value="Beschreibung:"/>
                <h:panelGroup>
                    <h:inputTextarea id="gameBeanGameDescription" value="#{gameBean.game.description}"/>
                    <h:message for="gameBeanGameDescription" styleClass="error"/>
                </h:panelGroup>
                <h:outputText/>




                <h:panelGroup>

                </h:panelGroup>


            </h:panelGrid>

            <h3>Cover:</h3>



            <div class="buttons">
                <h:commandLink value="Speichern" action="#{gameBean.update}" styleClass="btn btn-primary"/>
                <c:choose>
                    <c:when test="#{!empty gameBean.id}">
                        <h:link value="Abbrechen" outcome="view" styleClass="btn btn-primary">
                            <f:param name="id" value="#{gameBean.id}"/>
                        </h:link>
                        <h:commandLink value="Löschen" action="#{gameBean.delete}" styleClass="btn btn-primary"/>
                    </c:when>
                    <c:otherwise>
                        <h:link value="Abbrechen" outcome="search" styleClass="btn btn-primary"/>
                    </c:otherwise>
                </c:choose>
            </div>

            <p:fileUpload fileUploadListener="#{fileUploadBean.listener}"
                          mode="advanced"
                          auto="true"
                          id="uploader"
                          update=":create, uploader"
                          sizeLimit="100000"
                          multiple="true"
                          allowTypes="/(.|/)(gif|jpe?g|png)$/"/>
        </h:form>
    </ui:define>

</ui:composition>
答案

您需要将上载的附件文件保留在临时存储上。否则,您应该使用SessionScope支持bean。尝试如下,它是临时存储的示例。

我的页面

<h:form id="uploadForm" enctype="multipart/form-data">
    <p:fileUpload fileUploadListener="#{MyBean.handleAttachment}"  
                mode="advanced" update="imageGrid" multiple="true" sizeLimit="3000000"   
                allowTypes="/(.|/)(gif|jpe?g|png)$/" id="attachment"/><br/>
    <p:dataGrid id="imageGrid" var="image" value="#{MyBean.filePathList}">
        <p:graphicImage value="#{image}"/>
    </p:dataGrid>   
</h:form>

为myBean

private String temporyDir = "/temp/";
private String List<String> filePathList = new ArrayList<String>;

public List<String> getFilePathList() {
    return filePathList;
}

private String getWebAppRootPath() {
    Object context = getFacesContext().getExternalContext().getContext();
    String systemPath = ((ServletContext)context).getRealPath("/");
    return systemPath;
}

public void handleAttachment(FileUploadEvent event) {
    UploadedFile uploadedFile  = event.getFile();
    // You need to replace, white space character.
    String fileName = uploadedFile.getFileName().replaceAll("\s", "_");
    String filePath = temporyDir + "/" + fileName;
    filePathList.add(filePath);
    createFile(new File(getWebAppRootPath() + filePath), uploadedFile.getContents());
} 

private void createFile(File file, byte[] content) {
    try {
        /*At First : Create directory of target file*/
        String filePath = file.getPath();
        int lastIndex = filePath.lastIndexOf("\") + 1;
        FileUtils.forceMkdir(new File(filePath.substring(0, lastIndex)));

        /*Create target file*/
        FileOutputStream outputStream = new FileOutputStream(file);
        IOUtils.write(content, outputStream);
        outputStream.flush();
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}   

Here是上传的好参考

以上是关于刷新后PrimeFaces fileUploader不可见的主要内容,如果未能解决你的问题,请参考以下文章

上传文件的fileUpload和primefaces空值[重复]

PrimeFaces p:fileUpload 不调用方法

Primefaces fileUpload fileUploadListener问题

Primefaces:fileUpload显示Facesmessage无法正常工作

p:fileUpload 中的监听器方法永远不会在 primefaces 中调用 [重复]

如何在 PrimeFaces fileUpload 中向 fileUploadListener 发送参数