7 Replies Latest reply on Apr 13, 2009 11:50 AM by Sergio C

    Problems with FileUpload

    Sergio C Newbie

      Hello,

      First of all, thank you for all RichFaces team! great job!

      I´m trying to use the richFaces component fileUpload and doesn´t work correctly.

      I have copied the live demo code and I have the next problem. When I upload an image, the listener is working correctly but the onuploadevent doesn´t fire in my web page and the panelGroup component "info" doesn´t rendered. Furthermore, I have an strange behaviour in my back bean. The constructor is called more than one time and I lost the upload data.

      The order execution is the following:

      1º Upload file
      2º Back bean is called (two times!!!!!) New instances.
      3º The uploadListener is called and gets the data correctly.
      4º Back bean is called again two times !!! New instances.
      5º The method paint doesn´t called never.
      6º The upload component shows the info correctly but the panelGroup that contains the mediaOutput doesn´t working correctly

      Why my back bean is called more than one time!?!? Can I Have a config problem with richfaces!? I can save the image, but never show it..

      The version that I am using are the followings:

      RichFaces 3.3.0 GA and jsf-mojarra-1.2.12

      web.xml

      <web-app version="2.5"
       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-app_2_5.xsd">
      
       <description>debug web.xml</description>
      
       <context-param>
       <param-name>com.sun.faces.compressViewState</param-name>
       <param-value>enabled</param-value>
       </context-param>
      
       <context-param>
       <description>Comma separated list of URIs of (additional) faces config files.(e.g. /WEB-INF/my-config.xml) See JSF 1.0 PRD2, 10.3.2 Attention: You do not need to put /WEB-INF/faces-config.xml in here.</description>
       <param-name>javax.faces.CONFIG_FILES</param-name>
       <param-value>xxx</param-value>
       </context-param>
      
       <context-param>
       <description>State saving method: "client" or "server" (= default)
       See JSF Specification 2.5.3</description>
       <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
       <param-value>server</param-value>
       </context-param>
      
       <context-param>
       <description>
       This parameter enables partial state saving.
       </description>
       <param-name>javax.faces.PARTIAL_STATE_SAVING_METHOD</param-name>
       <param-value>false</param-value>
       </context-param>
      
       <context-param>
       <description>
       If true every time a page is rendered, the corresponding JSP is dispatched also.
       This is very usefull if Scriptlets are used inside the JSP.
       </description>
       <param-name>javax.faces.PARTIAL_STATE_SAVING_DISPATCH_EVERY_TIME</param-name>
       <param-value>false</param-value>
       </context-param>
      
       <context-param>
       <description>Makes Security View Handler printing debug messages at runtime</description>
       <param-name>com.groundside.jsf.security.print_debug_messages</param-name>
       <param-value>true</param-value>
      </context-param>
      
       <context-param>
       <description>Customize error message shown upon unauthorized access attempt</description>
       <param-name>com.groundside.jsf.security.unauthorized_access_error_messages</param-name>
       <param-value>User is not authorized to perform requested action</param-value>
      </context-param>
      
       <!-- Ajax4JSF filter -->
       <filter>
       <display-name>Ajax4jsf Filter</display-name>
       <filter-name>ajax4jsf</filter-name>
       <filter-class>org.ajax4jsf.Filter</filter-class>
       <init-param>
       <param-name>createTempFiles</param-name>
       <param-value>false</param-value>
       </init-param>
       <init-param>
       <param-name>maxRequestSize</param-name>
       <param-value>1000000</param-value>
       </init-param>
       </filter>
       <filter-mapping>
       <filter-name>ajax4jsf</filter-name>
       <servlet-name>Faces Servlet</servlet-name>
       <dispatcher>REQUEST</dispatcher>
       <dispatcher>FORWARD</dispatcher>
       <dispatcher>INCLUDE</dispatcher>
       </filter-mapping>
       <!-- Ajax4JSF filter -->
      
       <session-config>
       <session-timeout>120</session-timeout>
       </session-config>
      
      <listener>
      <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
      </listener>
      
       <servlet>
       <servlet-name>InitServlet</servlet-name>
       <servlet-class>com.xx.xxx.servlet.InitServlet</servlet-class>
       <init-param>
       <param-name>errorPage</param-name>
       <param-value>/jsp/error.jsp</param-value>
       </init-param>
      
       <load-on-startup>2</load-on-startup>
       </servlet>
      
       <servlet-mapping>
       <servlet-name>InitServlet</servlet-name>
       <url-pattern>/InitServlet</url-pattern>
       </servlet-mapping>
      
       <servlet>
       <servlet-name>Faces Servlet</servlet-name>
       <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
       <load-on-startup>1</load-on-startup>
       </servlet>
      
       <servlet>
       <servlet-name>SourceCodeServlet</servlet-name>
       <servlet-class>org.apache.myfaces.shared_tomahawk.util.servlet.SourceCodeServlet</servlet-class>
       </servlet>
       <servlet-mapping>
       <servlet-name>Faces Servlet</servlet-name>
       <url-pattern>*.jsf</url-pattern>
       </servlet-mapping>
      
       <servlet-mapping>
       <servlet-name>SourceCodeServlet</servlet-name>
       <url-pattern>*.source</url-pattern>
       </servlet-mapping>
      
       <welcome-file-list>
       <welcome-file>index.jsp</welcome-file>
       <welcome-file>index.html</welcome-file>
       </welcome-file-list>
      
       <error-page>
       <error-code>500</error-code>
       <location>/jsp/error500.jsf</location>
       </error-page>
       <error-page>
       <error-code>404</error-code>
       <location>/jsp/error404.jsf</location>
       </error-page>
       <error-page>
       <error-code>403</error-code>
       <location>/jsp/error403.jsf</location>
       </error-page>
       <error-page>
       <error-code>400</error-code>
       <location>/jsp/error400.jsf</location>
       </error-page>
      </web-app>
      


      any ideas???? Thanks!

        • 1. Re: Problems with FileUpload
          Ilya Shaikovsky Master

          unuploadcomplete not fired at all or just action not called during ajax request?

          1) add phaseTracker to your application if just action not called. And check its output for the final update request
          2) paste also bean ad page code
          3) getters for the beans properties is called many times during JSF lifecycle (especially for properties which bound to rendered attributes.).

          • 2. Re: Problems with FileUpload
            Sergio C Newbie

            Thanks for your answer!

            The bean and page code are the same that the live demo. it´s your code.

            My problem is that my constructor is called more than one time creating a new instance of my bean for each execution before collecting the data.

            When I upload one file, the bean is instantiated two times before the listener. when the listener is finished, the bean is instantiated two times again, and the method paint doesn´t called never.

            It´s an very strange behaviour. The listener is working (after two new instances of miy bean) but the onuploadevent and rerender don´t called never and don´t working well.

            Do you understand my problem? My english isn´t very good...

            Thanks!!!!!

            FileUploadBean.java

            package com.transhotel.TOR_PROVEEDORES.api.actions;
            
            import java.io.IOException;
             import java.io.OutputStream;
             import java.util.ArrayList;
            import java.util.List;
            
             import org.richfaces.event.UploadEvent;
            import org.richfaces.model.UploadItem;
            
             /**
             * @author Ilya Shaikovsky
             *
             */
             public class FileUploadBean{
            
             private ArrayList<File> files = new ArrayList<File>();
             private int uploadsAvailable = 1;
             private boolean autoUpload = false;
             private boolean useFlash = false;
             private List<UploadItem> filesUpload = new ArrayList<UploadItem>();
             public int getSize() {
             if (getFiles().size()>0){
             return getFiles().size();
             }else
             {
             return 0;
             }
             }
            
             public FileUploadBean() {
             System.out.println("constructor is called");
             }
            
             public synchronized void paint(OutputStream stream, Object object) throws IOException {
             stream.write(getFiles().get((Integer)object).getData());
             }
             public synchronized void listener(UploadEvent event) throws Exception{
             UploadItem item = event.getUploadItem();
             File file = new File();
             file.setLength(item.getData().length);
             file.setName(item.getFileName());
             file.setData(item.getData());
             files.add(file);
             uploadsAvailable--;
             }
            
             public String clearUploadData() {
             files.clear();
             setUploadsAvailable(5);
             return null;
             }
            
             public long getTimeStamp(){
             return System.currentTimeMillis();
             }
            
             public ArrayList<File> getFiles() {
             return files;
             }
            
             public void setFiles(ArrayList<File> files) {
             this.files = files;
             }
            
             public int getUploadsAvailable() {
             return uploadsAvailable;
             }
            
             public void setUploadsAvailable(int uploadsAvailable) {
             this.uploadsAvailable = uploadsAvailable;
             }
            
             public boolean isAutoUpload() {
             return autoUpload;
             }
            
             public void setAutoUpload(boolean autoUpload) {
             this.autoUpload = autoUpload;
             }
            
             public boolean isUseFlash() {
             return useFlash;
             }
            
             public void setUseFlash(boolean useFlash) {
             this.useFlash = useFlash;
             }
            
             public List<UploadItem> getFilesUpload() {
             return filesUpload;
             }
            
             public void setFilesUpload(List<UploadItem> filesUpload) {
             this.filesUpload = filesUpload;
             }
            }
            


            jsf code.

            <%@page contentType="text/html" pageEncoding="ISO-8859-1"%>
            <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
            <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
            <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
            <%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
            <%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
            <%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
            <html>
             <f:view>
            <head>
            <style>
            .top {
             vertical-align: top;
            
            }
            .info {
             height: 202px;
             overflow: auto;
            }
            </style>
            </head>
            <body>
            
             <a4j:log popup="false" level="ALL" style="width: 800px; height: 300px;"></a4j:log>
            
             <h:form>
             <h:panelGrid columns="2" columnClasses="top,top">
             <rich:fileUpload fileUploadListener="#{fileUploadBean.listener}"
             maxFilesQuantity="#{fileUploadBean.uploadsAvailable}"
             id="upload"
             immediateUpload="#{fileUploadBean.autoUpload}"
             acceptedTypes="jpg, gif, png, bmp" allowFlash="#{fileUploadBean.useFlash}"
             uploadData="#{fileUploadBean.filesUpload}">
             <a4j:support event="onuploadcomplete" reRender="info" />
             </rich:fileUpload>
             <h:panelGroup id="info">
             <rich:panel bodyClass="info">
             <f:facet name="header">
             <h:outputText value="Uploaded Files Info" />
             </f:facet>
             <h:outputText value="No files currently uploaded"
             rendered="#{fileUploadBean.size==0}" />
             <rich:dataGrid columns="1" value="#{fileUploadBean.files}"
             var="file" rowKeyVar="row">
             <rich:panel bodyClass="rich-laguna-panel-no-header">
             <h:panelGrid columns="2">
             <a4j:mediaOutput element="img" mimeType="#{file.mime}"
             createContent="#{fileUploadBean.paint}" value="#{row}"
             style="width:100px; height:100px;" cacheable="false">
             <f:param value="#{fileUploadBean.timeStamp}" name="time"/>
             </a4j:mediaOutput>
             <h:panelGrid columns="2">
             <h:outputText value="File Name:" />
             <h:outputText value="#{file.name}" />
             <h:outputText value="File Length(bytes):" />
             <h:outputText value="#{file.length}" />
             </h:panelGrid>
             </h:panelGrid>
             </rich:panel>
             </rich:dataGrid>
             </rich:panel>
             <rich:spacer height="3"/>
             <br />
             <a4j:commandButton action="#{fileUploadBean.clearUploadData}"
             reRender="info, upload" value="Clear Uploaded Data"
             rendered="#{fileUploadBean.size>0}" />
             </h:panelGroup>
             </h:panelGrid>
             </h:form>
            </body>
            </f:view>
            </html>
            


            • 3. Re: Problems with FileUpload
              Sergio C Newbie

              Here the phaseTracker info:

              I'm going crazy with this....

              13-abr-2009 13:06:59 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE RESTORE_VIEW 1
              13-abr-2009 13:06:59 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER RESTORE_VIEW 1
              13-abr-2009 13:06:59 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE RESTORE_VIEW 1
              13-abr-2009 13:06:59 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER RESTORE_VIEW 1
              13-abr-2009 13:06:59 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE APPLY_REQUEST_VALUES 2
              13-abr-2009 13:06:59 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE APPLY_REQUEST_VALUES 2
              13-abr-2009 13:06:59 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER APPLY_REQUEST_VALUES 2
              13-abr-2009 13:06:59 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE PROCESS_VALIDATIONS 3
              13-abr-2009 13:06:59 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER PROCESS_VALIDATIONS 3
              13-abr-2009 13:06:59 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE UPDATE_MODEL_VALUES 4
              13-abr-2009 13:06:59 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER UPDATE_MODEL_VALUES 4
              13-abr-2009 13:06:59 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE INVOKE_APPLICATION 5
              13-abr-2009 13:06:59 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER INVOKE_APPLICATION 5
              13-abr-2009 13:06:59 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE RENDER_RESPONSE 6
              13-abr-2009 13:07:00 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE RESTORE_VIEW 1
              13-abr-2009 13:07:00 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER RESTORE_VIEW 1
              13-abr-2009 13:07:00 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE APPLY_REQUEST_VALUES 2
              13-abr-2009 13:07:00 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER APPLY_REQUEST_VALUES 2
              13-abr-2009 13:07:00 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE PROCESS_VALIDATIONS 3
              13-abr-2009 13:07:00 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER PROCESS_VALIDATIONS 3
              13-abr-2009 13:07:00 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE UPDATE_MODEL_VALUES 4
              13-abr-2009 13:07:00 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER UPDATE_MODEL_VALUES 4
              13-abr-2009 13:07:00 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE INVOKE_APPLICATION 5
              13-abr-2009 13:07:00 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER INVOKE_APPLICATION 5
              13-abr-2009 13:07:00 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE RENDER_RESPONSE 6
              constructor is called
              13-abr-2009 13:07:31 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER APPLY_REQUEST_VALUES 2
              13-abr-2009 13:07:31 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE PROCESS_VALIDATIONS 3
              13-abr-2009 13:07:31 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER PROCESS_VALIDATIONS 3
              13-abr-2009 13:07:31 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE UPDATE_MODEL_VALUES 4
              13-abr-2009 13:07:31 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER UPDATE_MODEL_VALUES 4
              13-abr-2009 13:07:31 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE INVOKE_APPLICATION 5
              13-abr-2009 13:07:31 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER INVOKE_APPLICATION 5
              13-abr-2009 13:07:31 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE RENDER_RESPONSE 6
              13-abr-2009 13:07:31 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER RENDER_RESPONSE 6
              13-abr-2009 13:07:32 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE RESTORE_VIEW 1
              13-abr-2009 13:07:32 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER RESTORE_VIEW 1
              13-abr-2009 13:07:32 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE APPLY_REQUEST_VALUES 2
              constructor is called
              13-abr-2009 13:07:36 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER RENDER_RESPONSE 6
              constructor is called
              13-abr-2009 13:07:38 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER RENDER_RESPONSE 6
              constructor is called
              13-abr-2009 13:07:39 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER APPLY_REQUEST_VALUES 2
              13-abr-2009 13:07:39 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE PROCESS_VALIDATIONS 3
              13-abr-2009 13:07:39 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER PROCESS_VALIDATIONS 3
              13-abr-2009 13:07:39 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE UPDATE_MODEL_VALUES 4
              13-abr-2009 13:07:39 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER UPDATE_MODEL_VALUES 4
              13-abr-2009 13:07:39 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE INVOKE_APPLICATION 5
              13-abr-2009 13:07:39 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER INVOKE_APPLICATION 5
              13-abr-2009 13:07:39 org.exadel.jsf.PhaseTracker beforePhase
              INFO: BEFORE RENDER_RESPONSE 6
              13-abr-2009 13:07:39 org.exadel.jsf.PhaseTracker afterPhase
              INFO: AFTER RENDER_RESPONSE 6
              


              • 4. Re: Problems with FileUpload
                Ilya Shaikovsky Master

                change the scoped of the fileUploadBean to session.

                • 5. Re: Problems with FileUpload
                  Sergio C Newbie

                   

                  "ilya_shaikovsky" wrote:
                  change the scoped of the fileUploadBean to session.


                  Yes, I have changed the scoped of the component and it´s running correctly, but I don´t understand why the behaviour is incorrect when I setting the scoped to "request".

                  I prefer to work with request scope, putting the upload data in session.

                  This component is running well only when the scoped bean is session?????

                  Thanks for your answers!

                  • 6. Re: Problems with FileUpload
                    Ilya Shaikovsky Master

                    request scope is recreated on every request. and FileUpload sends separate requests for every file and the last request for update. So your data just gets lost.

                    • 7. Re: Problems with FileUpload
                      Sergio C Newbie

                      Thank you very much for your time and your answers!!!!!!

                      My issue has been resolved, putting the scope to session.

                      Have a nice day!