Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 刷新后PrimeFaces文件上载程序不可见_Jsf_File Upload_Primefaces - Fatal编程技术网

Jsf 刷新后PrimeFaces文件上载程序不可见

Jsf 刷新后PrimeFaces文件上载程序不可见,jsf,file-upload,primefaces,Jsf,File Upload,Primefaces,我有一个文件上传程序,它会在部分站点刷新后消失 <?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/xht

我有一个文件上传程序,它会在部分站点刷新后消失

<?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
。 尝试如下,这是临时存储的示例

我的网页

<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>


蚕豆

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();
    }
}   
私有字符串temporyDir=“/temp/”;
私有字符串列表filePathList=newarraylist;
公共列表getFilePathList(){
返回文件路径列表;
}
私有字符串getWebApprotPath(){
对象上下文=getFacesContext().getExternalContext().getContext();
字符串systemPath=((ServletContext)context.getRealPath(“/”);
返回系统路径;
}
public void handleAttachment(FileUploadEvent事件){
UploadedFile UploadedFile=event.getFile();
//您需要替换空白字符。
String fileName=uploadedFile.getFileName().replaceAll(“\\s”,“”);
字符串filePath=temporyDir+“/”+文件名;
添加(文件路径);
createFile(新文件(GetWebApprotPath()+文件路径),uploadedFile.getContents());
} 
私有void createFile(文件文件,字节[]内容){
试一试{
/*首先:创建目标文件的目录*/
字符串filePath=file.getPath();
int lastIndex=filePath.lastIndexOf(“\\”)+1;
FileUtils.forceMkdir(新文件(filePath.substring(0,lastIndex));
/*创建目标文件*/
FileOutputStream outputStream=新的FileOutputStream(文件);
写入(内容、输出流);
outputStream.flush();
outputStream.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}   
是一个很好的上传参考