jsf 2.0 Primefaces 多个 Fileupload 不记得上传的文件 [英] jsf 2.0 Primefaces multiple Fileupload does not remember uploaded files

查看:23
本文介绍了jsf 2.0 Primefaces 多个 Fileupload 不记得上传的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这篇文章中修复了一些其他问题(fileuploadhandler 现在正在工作),我的代码如下:

I already have fixed some other issues (fileuploadhandler is now working) in this post, where my code is following:

豆子:

@ManagedBean
@ViewScoped
public class TempBean implements Serializable{

    private static final long serialVersionUID = -70753689043016662L;
    private List<String> names; //to remember which files have been uploaded

    public void load(){
        System.out.println("@prerender");
    }

    public TempBean() {
        super();
        System.out.println("@constructor");
        names = new ArrayList<String>();
    }

    @PostConstruct
    public void pc(){
        System.out.println("@postconstruct");
    }

    public void handleFileUpload(FileUploadEvent event) { 
        String filename = event.getFile().getFileName();
        System.out.println("INCOMING FILE: " + filename);
        names.add(filename);

        if(!names.isEmpty()){
            System.out.println("Files uploaded:");
            for(String n : names){
                System.out.println("# " + n);
            }
            System.out.println();
        }
   }  

    public List<String> getNames() {
        return names; 
    }

    public void setNames(List<String> names) {
        this.names = names;
    }

}

xhtml 页面:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:p="http://primefaces.org/ui" >

    <h:head> 
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </h:head>

    <f:event type="preRenderView" listener="#{tempBean.load}" />
    <h:body>

        <h:form>
            <p:fileUpload
                fileUploadListener="#{tempBean.handleFileUpload}"
                mode="advanced" dragDropSupport="true" multiple="true" update=":overview" />
        </h:form>

        <h:panelGroup id="overview">
            <ui:repeat var="file" value="#{tempBean.names}">
                #{file} <br/>
            </ui:repeat>
        </h:panelGroup>

    </h:body>
</html>

我现在面临的问题(或者我打开一个新问题的原因)是,当我上传多个(例如三个)文件时,只有其中一个被附加到空列表中.当我重新上传剩下的两个文件时,同样,只有其中一个被添加到列表中.

The problem I am facing now (or the reason I open a new question), is that when I upload multiple (e.g. three) files, only one of them is appended to the empty list. when i reupload the two remaining files, again, only one of them is appended to the list.

有趣的是为每个文件调用 handleFileUpload(...)...

Interesting is that the handleFileUpload(...) is invoked for each file...

这是我同时上传三个文件时的控制台输出:

Here is my console-output when I upload three files simultaniously:

@constructor
@postconstruct
@prerender
INCOMING FILE: test1.jpe
INCOMING FILE: test3.jpe
Files uploaded:
INCOMING FILE: test2.jpe
# test3.jpe
-END OF LIST-
Files uploaded:
# test1.jpe
Files uploaded:
# test2.jpe
-END OF LIST-
-END OF LIST-
@prerender
@prerender
@prerender

如何才能使文件上传组件正常运行?这是错误还是 ajax 问题?

What can I do to make the fileupload-component run properly? is this a bug or an ajax problem?

这是我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0" >

  <display-name>TestProjekt</display-name>
  <welcome-file-list>
    <welcome-file>test.xhtml</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
    <servlet-mapping> 
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
        <param-value>resources.application</param-value>
    </context-param>
    <context-param>
        <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
    </context-param>
    <context-param>
        <param-name>facelets.SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>

    <context-param>
        <param-name>primefaces.UPLOADER</param-name>
        <param-value>commons</param-value>
    </context-param>

  <context-param>
    <description>
    This parameter tells MyFaces if javascript code should be allowed in
    the rendered HTML output.
    If javascript is allowed, command_link anchors will have javascript code
    that submits the corresponding form.
    If javascript is not allowed, the state saving info and nested parameters
    will be added as url parameters.
    Default is 'true'</description>
    <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
    <param-value>true</param-value>
  </context-param>

      <context-param> 
        <description> 
        If true, rendered HTML code will be formatted, so that it is 'human-readable'
        i.e. additional line separators and whitespace will be written, that do not
        influence the HTML code.
        Default is 'true'</description>
        <param-name>org.apache.myfaces.PRETTY_HTML</param-name>
        <param-value>true</param-value>
      </context-param>

      <context-param>
        <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
        <param-value>false</param-value>
      </context-param>

      <context-param>
        <description>
        If true, a javascript function will be rendered that is able to restore the
        former vertical scroll on every request. Convenient feature if you have pages
        with long lists and you do not want the browser page to always jump to the top
        if you trigger a link or button action that stays on the same page.
        Default is 'false'
        </description>
        <param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
        <param-value>true</param-value>
      </context-param>


    <filter> 
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
        <init-param>
            <param-name>uploadDirectory</param-name>
            <param-value>C:	mp</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>

</web-app>

和我的 faces.config:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0"> 

</faces-config>

推荐答案

这是由您的 JSF 实现 MyFaces 2.2.0-20130426 中的错误引起的.

This is caused by a bug in your JSF implementation MyFaces 2.2.0-20130426.

当我尝试当前在 myfaces.apache.org 上可用的 MyFaces 2.2.0-beta 时,它对我有用 和 Mojarra 2.2.4 作为当前可用的 javaserverfaces.java.net.

It works for me when I try MyFaces 2.2.0-beta as currently available at myfaces.apache.org and Mojarra 2.2.4 as currently available as javaserverfaces.java.net.

正如对该问题的评论所确认的那样,它也适用于您.

As confirmed by comments on the question, it then also works for you.

这篇关于jsf 2.0 Primefaces 多个 Fileupload 不记得上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆