刷新后PrimeFaces fileUploader不可见

时间:2013-07-14 13:46:58

标签: jsf file-upload primefaces

我有一个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>

1 个答案:

答案 0 :(得分:0)

您需要将上传的附件文件保留在临时存储中。否则,应使用支持bean SessionScope。 请尝试以下操作,它是临时存储的示例。

MYPAGE

<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是上传的好参考资料