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

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

问题描述

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

豆:

  @ManagedBean
@ViewScoped
公共类TempBean实现Serializable {

    私有静态最后长的serialVersionUID = -70753689043016662L;
    私人列表<字符串>名称; //记住哪些文件已被上传

    公共无效负载(){
        的System.out.println(@ prerender);
    }

    公共TempBean(){
        超();
        的System.out.println(@构造函数);
        名称=新的ArrayList<字符串>();
    }

    @PostConstruct
    公共无效PC(){
        的System.out.println(@ postconstruct);
    }

    公共无效handleFileUpload(FileUploadEvent事件){
        字符串文件名= event.getFile()getFileName()。
        的System.out.println(传入文件:+文件名);
        names.add(文件名);

        如果(!names.isEmpty()){
            的System.out.println(上传的文件:);
            对于(字符串n:名称){
                的System.out.println(#+ n)个;
            }
            的System.out.println();
        }
   }

    公开名单<字符串> getNames(){
        返回名称;
    }

    公共无效setNames(名单<字符串>的名字){
        this.names =名称;
    }

}
 

XHTML页面:

 <!DOCTYPE HTML>
< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml
        的xmlns:F =htt​​p://java.sun.com/jsf/core
        的xmlns:H =htt​​p://java.sun.com/jsf/html
        的xmlns:UI =htt​​p://java.sun.com/jsf/facelets
        的xmlns:P =htt​​p://primefaces.org/ui>

    < H:头>
        < META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= UTF-8/>
    < / H:头>

    < F:事件类型=preRenderView监听器=#{tempBean.load}/>
    < H:身体GT;

        < H:形式GT;
            <电话号码:文件上传
                fileUploadListener =#{tempBean.handleFileUpload}
                模式=高级dragDropSupport =真正的多=真正的更新=:概述/>
        < /小时:形式GT;

        < H:panelGroup中的id =概述>
            < UI:重复VAR =文件值=#{tempBean.names}>
                #{文件}< BR />
            < / UI:重复>
        < /小时:panelGroup中>

    < / H:身体GT;
< / HTML>
 

我现在面临的(或者我打开一个新的问题的原因),就是当我上传多个(如三级)的文件,其中只有一个是附加到空列表的问题。当我重新上传剩下的两个文件,​​再次,它们中只有一个被附加到列表中。

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

下面是我的控制台输出,当我上载三个文件同时放:

  @constructor
@PostConstruct
@ prerender
传入文件:test1.jpe
传入文件:test3.jpe
上传的文件:
传入文件:test2.jpe
#test3.jpe
列表 - 端起
上传的文件:
#test1.jpe
上传的文件:
#test2.jpe
列表 - 端起
列表 - 端起
@ prerender
@ prerender
@ prerender
 

我能做些什么,以使文件上传组件的运行是否正常?这是一个错误或一个Ajax的问题​​?

这是我的web.xml:

 < XML版本=1.0编码=UTF-8&GT?;
< web应用程序
的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance
的xmlns =htt​​p://java.sun.com/xml/ns/javaee
的xmlns:网络=htt​​p://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
XSI:的schemaLocation =htt​​p://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd
ID =WebApp_ID版本=3.0>

  <显示-名称>&TestProjekt LT; /显示-名称>
  <欢迎-文件列表>
    <欢迎-文件> test.xhtml< /欢迎-文件>
  < /欢迎-文件列表>
  <的servlet>
    < servlet的名称>面的Servlet< / Servlet的名称>
    <的servlet类> javax.faces.w​​ebapp.FacesServlet< / Servlet的类>
    &所述;负载上启动→1&其中; /负载上启动>
  < / servlet的>

    < Servlet映射>
        < servlet的名称>面的Servlet< / Servlet的名称>
        < URL模式> /面/ *< / URL模式>
    < / Servlet映射>
    < Servlet映射>
        < servlet的名称>面的Servlet< / Servlet的名称>
        < URL模式> * JSF< / URL模式>
    < / Servlet映射>
    < Servlet映射>
        < servlet的名称>面的Servlet< / Servlet的名称>
        < URL模式> *。面对< / URL模式>
    < / Servlet映射>
    < Servlet映射>
        < servlet的名称>面的Servlet< / Servlet的名称>
        < URL模式> * XHTML< / URL模式>
    < / Servlet映射>

    <的context-param>
        <参数 - 名称>&javax.servlet.jsp.jstl.fmt.localizationContext LT; /参数 - 名称>
        <参数值> resources.application< /参数值>
    < /的context-param>
    <的context-param>
        <描述>国家节能方法:客户端或服务器(默认设置)。见JSF规范2.5.2< /描述>
        <参数 - 名称>&javax.faces.STATE_SAVING_METHOD LT; /参数 - 名称>
        <参数值>客户端和LT; /参数值>
    < /的context-param>
    <的context-param>
        <参数 - 名称>&facelets.SKIP_COMMENTS LT; /参数 - 名称>
        <参数值>真< /参数值>
    < /的context-param>

    <的context-param>
        <参数 - 名称>&primefaces.UPLOADER LT; /参数 - 名称>
        <参数值>公< /参数值>
    < /的context-param>

  <的context-param>
    <描述>
    这个参数告诉MyFaces的,如果JavaScript的code应该被允许
    所提供的HTML输出。
    如果JavaScript是允许的,command_link锚将JavaScript的code
    即提交相应的表格。
    如果JavaScript是不允许的,节约的信息和嵌套的参数状态
    将作为链接参数。
    默认值是'真'< /描述>
    <参数 - 名称>&org.apache.myfaces.ALLOW_JAVASCRIPT LT; /参数 - 名称>
    <参数值>真< /参数值>
  < /的context-param>

      <的context-param>
        <描述>
        如果为真,呈现的HTML code将被格式化,所以它是人类可读
        即额外的行分隔符和空白将被写入,不这样做
        影响HTML code。
        默认值是'真'< /描述>
        <参数 - 名称> org.apache.myfaces preTTY_HTML< /参数 - 名称>
        <参数值>真< /参数值>
      < /的context-param>

      <的context-param>
        <参数 - 名称>&org.apache.myfaces.DETECT_JAVASCRIPT LT; /参数 - 名称>
        <参数值>假< /参数值>
      < /的context-param>

      <的context-param>
        <描述>
        如果为true,JavaScript函数将被渲染即能恢复
        在每次请求前垂直滚动。方便的功能,如果你有网页
        长列表,你不希望浏览器页面总是跳到顶端
        如果触发停留在同一页面上的链接或按钮动作。
        默认为假
        < /描述>
        <参数 - 名称>&org.apache.myfaces.AUTO_SCROLL LT; /参数 - 名称>
        <参数值>真< /参数值>
      < /的context-param>


    &其中;滤光器>
        <过滤器名称> PrimeFaces文件上传过滤和LT; /过滤器名称>
        <过滤器类和GT; org.primefaces.w​​ebapp.filter.FileUploadFilter< /过滤器级>
        <初始化参数>
            <参数 - 名称>&uploadDirectory LT; /参数 - 名称>
            <参数值> C:\ tmp目录< /参数值>
        < /初始化参数>
    < /滤光器>
    <过滤器映射>
        <过滤器名称> PrimeFaces文件上传过滤和LT; /过滤器名称>
        < servlet的名称>面的Servlet< / Servlet的名称>
    < /过滤器映射>

< / web-app的>
 

我faces.config:

 < XML版本=1.0编码=UTF-8&GT?;
<脸,配置
    的xmlns =htt​​p://java.sun.com/xml/ns/javaee
    的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance
    XSI:的schemaLocation =htt​​p://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd
    版本=2.0>

< /面,配置>
 

解决方案

这是一个错误在你的JSF实现的MyFaces 2.2.0-20130426引起的。

这为我工作,当我尝试MyFaces的2.2.0-beta版目前可在 myfaces.apache.org 并Mojarra 2.2.4作为目前作为 javaserverfaces.java.net

证实了关于这个问题的意见,然后,它也适用于你。

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

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

}

xhtml-page:

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

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

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

here is my 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:\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>

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

解决方案

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

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多文件上传不记得上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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