当前位置: 首页 > 面试题库 >

jsf 2.0 Primefaces多个Fileupload不记得上传的文件

冯庆
2023-03-14
问题内容

我已经在本文中解决了其他一些问题(fileuploadhandler现在正在工作),其中我的代码如下:

豆:

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

我现在面临的问题(或我提出一个新问题的原因)是,当我上传多个(例如三个)文件时,只有其中一个被追加到空列表中。当我重新上传剩余的两个文件时,再次,只有其中一个附加到列表中。

有趣的是为每个文件都调用handleFileUpload(…)。

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

@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

如何使fileupload-component正常运行?这是bug还是ajax问题?

这是我的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>

和我的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中的错误引起的。

当我尝试使用MyFaces
2.2.0-beta(目前可在myfaces.apache.org上使用)和Mojarra
2.2.4(目前可通过javaserverfaces.java.net上使用)时,它对我有用。

正如对问题的评论所证实的,它也可以为您服务。



 类似资料:
  • 本文向大家介绍java组件fileupload文件上传demo,包括了java组件fileupload文件上传demo的使用技巧和注意事项,需要的朋友参考一下 在我们的web开发中,很多的时候都需要把本机的一些文件上传到web服务器上面去。 如:一个BBS系统,当用户使用这是系统的时候,能把本机的一些图片,文档上传到服务器上面去。然后其他用户可以去下载这些文件,那么这样的话,我们可以自己编程实现文

  • 本文向大家介绍Asp.Net的FileUpload类实现上传文件实例,包括了Asp.Net的FileUpload类实现上传文件实例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Asp.Net的FileUpload类实现上传文件的方法。分享给大家供大家参考。 具体功能代码如下: 希望本文所述对大家的asp.net程序设计有所帮助。

  • 我有一个多,我需要在服务器上上传1到N个文件,上传后我需要知道上传了多少个文件,然后开始处理方法。我怎么知道所有文件何时上传?我没有找到任何多次上传的事件,只有单个文件有事件,中的也适用于每个文件单独。我有想法使用附加按钮来调用处理方法,但这不安全,因为用户可以在所有文件上传之前按此按钮。谁能给我一些建议?

  • 本文向大家介绍JQuery fileupload插件实现文件上传功能,包括了JQuery fileupload插件实现文件上传功能的使用技巧和注意事项,需要的朋友参考一下 道理相通,我简单分享下在.net MVC下的实装。 1.制作Model类 2.实装controller里的对应方法,我这个处理逻辑比较复杂,懒得修改了,反正就这个意思 3.页面的实装 √,就是这个样子 还附赠了一个简易lodin

  • 本文向大家介绍Java组件FileUpload上传文件实现代码,包括了Java组件FileUpload上传文件实现代码的使用技巧和注意事项,需要的朋友参考一下 一般在用Servlet处理表单元素时,表单元素都是一些简单的文本,Servlet很容易用Request.getParameter()就可以处理。但是当表单不止包含一些简单的文本,比如有上传文件域时,Servlet直接从HttpServlet

  • 问题内容: 我正在尝试使用“流式” Apache Commons File Upload API上传大文件。 我使用Apache Commons File Uploader而不是默认的Spring Multipart Uploader的原因是,当我们上传非常大的文件大小(〜2GB)时,它失败了。我在GIS应用程序上工作,这种文件上传非常常见。 我的文件上传控制器的完整代码如下: 麻烦的是,始终返回