Ajax jsf 2.0 Primefaces multiple Fileupload不记得上载的文件

Ajax jsf 2.0 Primefaces multiple Fileupload不记得上载的文件,ajax,file-upload,jsf-2,primefaces,drag-and-drop,Ajax,File Upload,Jsf 2,Primefaces,Drag And Drop,在这篇文章中,我已经修复了一些其他问题(fileuploadhandler正在工作),我的代码如下: Bean: @ManagedBean @ViewScoped public class TempBean implements Serializable{ private static final long serialVersionUID = -70753689043016662L; private List<String> names; //to remembe

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

Bean:

@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;
    }

}
<!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>
<?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:\tmp</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>

</web-app>
<?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>
如何使fileupload组件正常运行?这是一个bug还是ajax问题

这是我的web.xml:

@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;
    }

}
<!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>
<?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:\tmp</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>

</web-app>
<?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>

测试项目
test.xhtml
Facesservlet
javax.faces.webapp.FacesServlet
1.
Facesservlet
/面孔/*
Facesservlet
*.jsf
Facesservlet
*.面孔
Facesservlet
*.xhtml
javax.servlet.jsp.jstl.fmt.localizationContext
资源.应用
状态保存方法:“客户端”或“服务器”(=默认值)。参见JSF规范2.5.2
javax.faces.STATE_保存方法
客户
facelets.SKIP_注释
真的
primefaces.UPLOADER
平民
此参数告诉MyFaces是否应允许使用javascript代码
呈现的HTML输出。
如果允许使用javascript,则命令链接锚定将具有javascript代码
提交相应的表格。
如果不允许使用javascript,则保存信息和嵌套参数的状态
将作为url参数添加。
默认值为“true”
org.apache.myfaces.ALLOW_JAVASCRIPT
真的
如果为true,则将格式化呈现的HTML代码,使其“可读”
i、 e.将写入额外的行分隔符和空格,但不会
影响HTML代码。
默认值为“true”
org.apache.myfaces.PRETTY_HTML
真的
org.apache.myfaces.DETECT\u JAVASCRIPT
假的
如果为true,则将呈现一个能够恢复
前垂直滚动每个请求。方便的功能,如果你有网页
对于长列表,您不希望浏览器页面总是跳转到顶部
如果您触发的链接或按钮操作保持在同一页面上。
默认值为“false”
org.apache.myfaces.AUTO_滚动
真的
PrimeFaces文件上载筛选器
org.primefaces.webapp.filter.FileUploadFilter
上传目录
C:\tmp
PrimeFaces文件上载筛选器
Facesservlet
和my faces.config:

@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;
    }

}
<!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>
<?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:\tmp</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>

</web-app>
<?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中的错误引起的

当我尝试MyFaces 2.2.0-beta版(目前在上提供)和Mojarra 2.2.4版(目前在上提供)时,它对我很有用


正如对这个问题的评论所证实的,它也适用于您。

我是唯一一个有这个问题的人吗?确切地说是哪个JSF impl/版本,确切地说是哪个PF版本?请不要说“JSF2.0”。这是一个规范版本,它没有告诉任何关于正在使用的实现及其(子)版本的信息。另请参见Hi的顶部部分,我目前正在使用PF 4.0(社区下载)和JSF 2.0 Apache Myfaces-2.2.0-20130426。(Tomcat 7)好的,你的
faces config.xml
的根声明到底是什么样子的,你们在
web.xml
中对PrimeFaces文件上传配置声明了什么?您好,我刚刚尝试使用MyFaces 2.2.0测试版复制它,因为我无法复制它,它对我来说很好。我找不到您在那里使用的20130426版本,但日期表明是早期的alpha版本。我建议你选择最新的版本(2.2.0测试版),试试看。