Primefaces 5.2 Fileupload不记得文件名

时间:2015-08-09 19:23:04

标签: jsf file-upload primefaces jsf-2.2

亲爱的Stackoverflow社区,

这个问题与以下problem described几乎相同。区别在于我已经更新了我的JSF实现,但我仍然得到了相同的行为。在我的情况下,可能导致列表的原因是不保留以前上传的名称?

我正在使用PrimeFaces 5.2社区版,Glassfish4和Mojarra 2.2.12在创建此问题时最新版本的2.2.x分支。

2015-08-08T20:41:42.748-0400|INFO: Initializing Mojarra 2.2.12 ( 20150720-0848 https://svn.java.net/svn/mojarra~svn/tags/2.2.12@14885) for context '/Pack_Encrypt'
2015-08-08T20:41:44.630-0400|INFO: Monitoring jndi:/server/Pack_Encrypt/WEB-INF/faces-config.xml for modifications
2015-08-08T20:41:44.666-0400|INFO: Running on PrimeFaces 5.2

如果可以的话,我想向您展示我的配置文件和我的代码。

  • FileMultipleBean:
    @ManagedBean
    @ViewScoped
    public class FileMultipleBean implements Serializable {
        private static final long serialVersionUID = 1672580425686966809L;
        private List<String> names;



        public FileMultipleBean() {
            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.w3c.org/1999/xhtml"
     xmlns:h="http://xmlns.jcp.org/jsf/html"
     xmlns:p="http://primefaces.org/ui">
   <h:head></h:head>
   <h:body>

      <h:form>
          <p:fileUpload  
                     fileUploadListener="#{fileMultipleBean.handleFileUpload}" 
                     mode="advanced" 
                     dragDropSupport="false"
                     multiple="true"
                         update="messages" 
                         sizeLimit="100000" 
                         fileLimit="3" 
                         allowTypes="/(\.|\/)(gif|jpe?g|png|txt)$/" 
          />

          <p:growl id="messages" showDetail="true" />
      </h:form>      
   </h:body>

   <p:dialog modal="true" widgetVar="statusDialog" header="Status" draggable="false" closable="false" resizable="false">
       <p:graphicImage name="images/ajaxloadingbar.gif" />
   </p:dialog>

   <script type="text/javascript">
      function start() {
          PF('statusDialog').show();
      }


      function stop() {
          PF('statusDialog').hide();
      }
   </script>
</html>

当我同时上传三个文件时,这是我的控制台输出,你会看到文件没有保存在列表中:

控制台输出

2015-08-08T20:43:23.468-0400|INFO: @constructor
2015-08-08T20:43:26.182-0400|INFO: @constructor
2015-08-08T20:43:32.350-0400|INFO: @postconstruct
2015-08-08T20:43:40.868-0400|INFO: @constructor
2015-08-08T20:43:44.213-0400|INFO: @postconstruct
2015-08-08T20:43:54.936-0400|INFO: INCOMING FILE: PKG_AUDIT.txt
2015-08-08T20:43:59.483-0400|INFO: Files uploaded:
2015-08-08T20:44:02.985-0400|INFO: # PKG_AUDIT.txt
2015-08-08T20:44:13.391-0400|INFO: @postconstruct
2015-08-08T20:44:19.255-0400|INFO: INCOMING FILE: PKG_AUDIT_ALERT.txt
2015-08-08T20:44:20.654-0400|INFO: Files uploaded:
2015-08-08T20:44:22.886-0400|INFO: # PKG_AUDIT_ALERT.txt
2015-08-08T20:44:31.806-0400|INFO: INCOMING FILE: Test SQL.txt
2015-08-08T20:44:39.194-0400|INFO: Files uploaded:
2015-08-08T20:44:41.946-0400|INFO: # Test SQL.txt

我的Web.xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>Pack_Encrypt</display-name>

  <welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
  </welcome-file-list>

  <filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
  </filter-mapping>


  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>

  <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>*.xhtml</url-pattern>
  </servlet-mapping>
</web-app>

我的faces.config像这样:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">

</faces-config>

如果我在第一次加载@ViewScoped页面时更改@SessionScoped index.xhtml并上传三个文件,行为是相同的,那么如果我刷新页面并再试一次,列表将包含第一次尝试的最后一项,其他三项,检查下一个控制台输出:

首次上传

2015-08-10T10:15:53.530-0400|INFO: @postconstruct
2015-08-10T10:15:53.604-0400|INFO: INCOMING FILE: PKG_AUDIT.txt
2015-08-10T10:15:54.506-0400|INFO: Files uploaded:
2015-08-10T10:15:54.506-0400|INFO: # PKG_AUDIT.txt
2015-08-10T10:15:55.606-0400|INFO: @postconstruct
2015-08-10T10:15:56.149-0400|INFO: INCOMING FILE: PKG_AUDIT_ALERT.txt
2015-08-10T10:15:56.587-0400|INFO: Files uploaded:
2015-08-10T10:15:56.587-0400|INFO: # PKG_AUDIT_ALERT.txt
2015-08-10T10:15:57.038-0400|INFO: @postconstruct
2015-08-10T10:15:57.449-0400|INFO: INCOMING FILE: Test SQL.txt
2015-08-10T10:15:57.989-0400|INFO: Files uploaded:
2015-08-10T10:15:57.989-0400|INFO: # Test SQL.txt    

第二次上传

2015-08-10T10:25:06.850-0400|INFO: INCOMING FILE: 1.txt
2015-08-10T10:25:09.215-0400|INFO: Files uploaded:
2015-08-10T10:25:11.119-0400|INFO: # Test SQL.txt
2015-08-10T10:25:11.999-0400|INFO: # 1.txt
2015-08-10T10:25:15.939-0400|INFO: INCOMING FILE: 2.txt
2015-08-10T10:25:16.488-0400|INFO: Files uploaded:
2015-08-10T10:25:16.488-0400|INFO: # Test SQL.txt
2015-08-10T10:25:16.488-0400|INFO: # 1.txt
2015-08-10T10:25:16.488-0400|INFO: # 2.txt
2015-08-10T10:25:16.888-0400|INFO: INCOMING FILE: 3.txt
2015-08-10T10:25:17.189-0400|INFO: Files uploaded:
2015-08-10T10:25:17.189-0400|INFO: # Test SQL.txt
2015-08-10T10:25:17.189-0400|INFO: # 1.txt
2015-08-10T10:25:17.189-0400|INFO: # 2.txt
2015-08-10T10:25:17.189-0400|INFO: # 3.txt

我检查了Cookie,正如@BalusC建议的那样,以下说明中的数据可能会有所帮助,39的大小,域名,路径,过期/最大年龄设置为N/A

Request Cookies                 39              
JSESSIONID  7f6179234f8ec0b82e482901bfff    N/A N/A N/A 39  

Response                        0

0 个答案:

没有答案