1 2 Previous Next 20 Replies Latest reply on May 12, 2010 12:11 PM by kem

    RichFaces fileUpload

    kem

      Hi,
      can't find any working example for rich fileUpload with seam. If someone succeed to get it work, could please help. Apparently what is supposed to work seamlessly in seam is not working !


      Thanks

        • 1. Re: RichFaces fileUpload
          sappo

          Hi Khalil,


          try this:


          .xhtml:


          <rich:fileUpload fileUploadListener="#{uploadAction.listener}"
                           maxFilesQuantity="#{uploadAction.uploadsAvailable}" 
                           id="upload" acceptedTypes="#{uploadAction.acceptedTypes}"
                           immediateUpload="true" listWidth="300" listHeight="60">
              <a:support event="onclear" action="#{uploadAction.clearFile}"   
                         reRender="arbeitsmittelField" />
          </rich:fileUpload>
          


          event listener:


          public void listener(UploadEvent event) throws Exception {
             uploadedFile = event.getUploadItem();
             uploadsAvailable--;
          }
          


          clear action:


          public void clearFile() {
              uploadedFile = null;
              uploadsAvailable++;
          }
          



          //Kevin

          • 2. Re: RichFaces fileUpload
            kem

            Thanks Kevin,


            the Add button is disabled!!!


            Khalil

            • 3. Re: RichFaces fileUpload
              kem

              the Add button is enabled. The uploaded object is always null!
              Any idea?

              • 4. Re: RichFaces fileUpload
                babazs
                Try this:

                <h:form>
                <rich:fileUpload
                addControlLabel="upload"                                                  
                fileUploadListener="#{uploadAction.listener}"                                     acceptedTypes="jpg,jpeg,gif,png"          
                allowFlash="auto"/>
                </h:form>
                web.xml:

                     <filter>
                          <display-name>RichFaces Filter</display-name>
                          <filter-name>richfaces</filter-name>
                          <filter-class>org.ajax4jsf.Filter</filter-class>
                          <init-param>
                               <param-name>createTempFiles</param-name>
                               <param-value>true</param-value>
                          </init-param>
                          <init-param>
                               <param-name>maxRequestSize</param-name>
                               <param-value>2222222</param-value>
                          </init-param>
                     </filter>
                     <filter-mapping>
                          <filter-name>richfaces</filter-name>
                          <servlet-name>Faces Servlet</servlet-name>
                          <dispatcher>REQUEST</dispatcher>
                          <dispatcher>FORWARD</dispatcher>
                          <dispatcher>INCLUDE</dispatcher>
                     </filter-mapping>


                components.xml:
                     <web:multipart-filter create-temp-files="false" max-request-size="1048700"/>

                • 5. Re: RichFaces fileUpload
                  babazs
                  The RichFaces demo page is a good starting point:
                  http://livedemo.exadel.com/richfaces-demo/richfaces/fileUpload.jsf;jsessionid=7789A83321737F7459812F8339900399?c=fileUpload&tab=usage
                  • 6. Re: RichFaces fileUpload
                    kem

                    Thanks Balázs


                    I still got this exception:
                    fileUploadListener= #{upload.listener}: Method not found: UploadBean:5c4o1b-pc6omx-g8t12abv-1-g8t1p2a7-l.listener(org.richfaces.event.UploadEvent);
                    and of course UploadItem null.


                    The RichFaces demo is for demos outside seam. I have the feeling that this problem is linked to seam + Richfaces usage.


                    Khalil

                    • 7. Re: RichFaces fileUpload
                      boomerbrian.brian.abston.featurenotbug.com

                      @Khalil - Post your xhtml and java code so we can take a look at it.

                      • 8. Re: RichFaces fileUpload
                        kem

                        Thanks Brian,


                        @Local
                        public interface Upload {
                        //public void listener(UploadEvent event) throws Exception -> has no effect
                            public void upload();
                            public String getFileName();
                                public void setFileName(String fileName);
                                public byte[] getData();
                                public void setData(byte[] data);
                            public void destroy();
                        }




                        @Stateful
                        @Name("upload")
                        public class UploadBean implements Upload {
                        
                                @Logger private Log log;
                                @In
                                StatusMessages statusMessages;
                                private String fileName;
                                private byte[] data;
                                UploadItem item;
                        
                                public void listener(UploadEvent event) throws Exception {
                                        item = event.getUploadItem();
                                }
                        
                                public void upload() {
                                        statusMessages.add("upload item" + item); item always null
                                }
                        
                                @Remove
                                public void destroy() {
                                }
                        
                                public String getFileName() {
                                        return fileName;
                                }
                        
                                public void setFileName(String fileName) {
                                        this.fileName = fileName;
                                }
                        
                                public byte[] getData() {
                                        return data;
                                }
                        
                                public void setData(byte[] data) {
                                        this.data = data;
                                }




                        <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                        <ui:composition xmlns="http://www.w3.org/1999/xhtml"
                                                        xmlns:s="http://jboss.com/products/seam/taglib"
                                                        xmlns:ui="http://java.sun.com/jsf/facelets"
                                                        xmlns:f="http://java.sun.com/jsf/core"
                                                        xmlns:h="http://java.sun.com/jsf/html"
                                                        xmlns:rich="http://richfaces.org/rich"
                                                        xmlns:a="http://richfaces.org/a4j"
                                                        template="layout/template.xhtml">
                                <ui:define name="body">
                                        <h:form id="uploadForm" enctype="multipart/form-data">
                                                        <f:facet name="header">upload</f:facet>
                                                        <rich:fileUpload
                                                                addControlLabel="upload"
                                                                fileUploadListener="#{upload.listener}"
                                                                acceptedTypes="jpg,jpeg,gif,png"
                                                                allowFlash="auto"/>
                                                <div class="actionButtons">
                                                        <h:commandButton id="upload" value="upload"
                                                                                         action="#{upload.upload}"/>
                                                </div>
                                        </h:form>
                                </ui:define>
                        </ui:composition>





                        web.xml
                        -------


                        <?xml version="1.0" ?>
                        <web-app 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"
                           version="2.5">
                        
                           <!-- RichFaces -->
                        
                           <context-param>
                              <param-name>org.richfaces.SKIN</param-name>
                              <param-value>classic</param-value>
                           </context-param>
                        
                           <!-- Suppress spurious stylesheets -->
                        
                           <context-param>
                              <param-name>org.richfaces.CONTROL_SKINNING</param-name>
                              <param-value>disable</param-value>
                           </context-param>
                        
                           <context-param>
                              <param-name>org.richfaces.CONTROL_SKINNING_CLASSES</param-name>
                              <param-value>disable</param-value>
                           </context-param>
                        
                           <!-- Change load strategy to DEFAULT to disable sending scripts/styles as packs -->
                        
                           <context-param>
                              <param-name>org.richfaces.LoadStyleStrategy</param-name>
                              <param-value>ALL</param-value>
                           </context-param>
                        
                           <context-param>
                              <param-name>org.richfaces.LoadScriptStrategy</param-name>
                              <param-value>ALL</param-value>
                           </context-param>
                        
                        
                        <filter>
                          <display-name>RichFaces Filter</display-name>
                          <filter-name>richfaces</filter-name>
                          <filter-class>org.ajax4jsf.Filter</filter-class>
                          <init-param>
                           <param-name>createTempFiles</param-name>
                           <param-value>true</param-value>
                          </init-param>
                          <init-param>
                           <param-name>maxRequestSize</param-name>
                           <param-value>2222222</param-value>
                          </init-param>
                         </filter>
                         <filter-mapping>
                          <filter-name>richfaces</filter-name>
                          <servlet-name>Faces Servlet</servlet-name>
                          <dispatcher>REQUEST</dispatcher>
                          <dispatcher>FORWARD</dispatcher>
                          <dispatcher>INCLUDE</dispatcher>
                         </filter-mapping>
                        
                        
                        
                           <!-- Seam -->
                        
                           <listener>
                              <listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
                           </listener>
                        
                           <filter>
                              <filter-name>Seam Filter</filter-name>
                              <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
                           </filter>
                        
                           <filter-mapping>
                              <filter-name>Seam Filter</filter-name>
                              <url-pattern>/*</url-pattern>
                           </filter-mapping>
                        
                           <servlet>
                              <servlet-name>Seam Resource Servlet</servlet-name>
                              <servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>
                           </servlet>
                        
                           <servlet-mapping>
                              <servlet-name>Seam Resource Servlet</servlet-name>
                              <url-pattern>/seam/resource/*</url-pattern>
                           </servlet-mapping>
                        
                           <!-- Facelets development mode (disable in production) -->
                        
                           <context-param>
                              <param-name>facelets.DEVELOPMENT</param-name>
                              <param-value>@debug@</param-value>
                           </context-param>
                        
                           <!-- JSF -->
                        
                           <context-param>
                              <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
                              <param-value>.xhtml</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>*.seam</url-pattern>
                           </servlet-mapping>
                        
                           <security-constraint>
                              <display-name>Restrict raw XHTML Documents</display-name>
                              <web-resource-collection>
                                 <web-resource-name>XHTML</web-resource-name>
                                 <url-pattern>*.xhtml</url-pattern>
                              </web-resource-collection>
                              <auth-constraint/>
                           </security-constraint>
                        
                           <!-- uncomment <ejb-local-ref> entries when deploying to GlassFish and (optionally) JBoss AS 5 -->
                           <!--
                           <ejb-local-ref>
                              <ejb-ref-name>fileUpload/AuthenticatorBean/local</ejb-ref-name>
                              <ejb-ref-type>Session</ejb-ref-type>
                              <local-home/>
                              <local>com.kem.ae.action.Authenticator</local>
                           </ejb-local-ref>
                           -->
                        
                           <!-- Add entries for each EJB session bean which is also a Seam component (not required on JBoss AS) -->
                        
                           <persistence-unit-ref>
                              <persistence-unit-ref-name>fileUpload/pu</persistence-unit-ref-name>
                              <persistence-unit-name>../fileUpload.jar#fileUpload</persistence-unit-name>
                              <!-- The relative reference doesn't work on GlassFish. Instead, set the <persistence-unit-name> to "fileUpload",
                                   package persistence.xml in the WAR, and add a <jar-file> element in persistence.xml with value "../../fileUpload.jar".
                              <persistence-unit-name>fileUpload</persistence-unit-name>
                              -->
                           </persistence-unit-ref>
                        
                        </web-app>
                        



                        componenet.xml
                        --------------


                        <?xml version="1.0" encoding="UTF-8"?>
                        <components xmlns="http://jboss.com/products/seam/components"
                                    xmlns:core="http://jboss.com/products/seam/core"
                                    xmlns:persistence="http://jboss.com/products/seam/persistence"
                                    xmlns:drools="http://jboss.com/products/seam/drools"
                                    xmlns:bpm="http://jboss.com/products/seam/bpm"
                                    xmlns:security="http://jboss.com/products/seam/security"
                                    xmlns:mail="http://jboss.com/products/seam/mail"
                                    xmlns:web="http://jboss.com/products/seam/web"
                                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                    xsi:schemaLocation=
                                        "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.2.xsd
                                         http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.2.xsd
                                         http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.2.xsd
                                         http://jboss.com/products/seam/bpm http://jboss.com/products/seam/bpm-2.2.xsd
                                         http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.2.xsd
                                         http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.2.xsd
                                         http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.2.xsd
                                         http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.2.xsd">
                        
                           <core:init debug="@debug@" jndi-pattern="@jndiPattern@"/>
                        
                           <core:manager concurrent-request-timeout="500"
                                         conversation-timeout="120000"
                                         conversation-id-parameter="cid"
                                         parent-conversation-id-parameter="pid"/>
                        
                           <!-- Make sure this URL pattern is the same as that used by the Faces Servlet -->
                           <web:hot-deploy-filter url-pattern="*.seam"/>
                        
                           <persistence:managed-persistence-context name="entityManager" auto-create="true"
                                              persistence-unit-jndi-name="@puJndiName@"/>
                        
                           <drools:rule-base name="securityRules">
                              <drools:rule-files>
                                 <value>/security.drl</value>
                              </drools:rule-files>
                           </drools:rule-base>
                        
                           <security:rule-based-permission-resolver security-rules="#{securityRules}"/>
                        
                           <security:identity authenticate-method="#{authenticator.authenticate}" remember-me="true"/>
                        
                           <event type="org.jboss.seam.security.notLoggedIn">
                              <action execute="#{redirect.captureCurrentView}"/>
                           </event>
                           <event type="org.jboss.seam.security.loginSuccessful">
                              <action execute="#{redirect.returnToCapturedView}"/>
                           </event>
                        
                           <mail:mail-session host="localhost" port="25"/>
                        
                           <!-- For use with jBPM pageflow or process management -->
                           <!--
                           <bpm:jbpm>
                              <bpm:process-definitions></bpm:process-definitions>
                              <bpm:pageflow-definitions></bpm:pageflow-definitions>
                           </bpm:jbpm>
                           -->
                        
                        <component class="org.jboss.seam.web.MultipartFilter">
                          <property name="createTempFiles">true</property>
                          <property name="maxRequestSize">10000000</property>
                        </component>
                        
                        <web:multipart-filter create-temp-files="false" max-request-size="1048700"/>
                        </components>
                        












                        • 9. Re: RichFaces fileUpload
                          sappo

                          Hi Khalil,


                          I'm not quite sure what the problem is, but to me it seems like the trigger of your problem is that your UploadBean doesn't have a Scope.


                          Try:

                          @Scope(ScopeType.PAGE) or

                          @Scope(ScopeType.SESSION) if you need the state of your bean for longer.


                          //Kevin

                          • 10. Re: RichFaces fileUpload
                            kem

                            Hi Kevin,


                            tried with @Scope. Always null item.


                            If it is working for you, could you please post you code.
                            I fellowed exactly what is in RichFaces manual. No success.


                            Regards

                            • 11. Re: RichFaces fileUpload
                              boomerbrian.brian.abston.featurenotbug.com

                              Nothing really jumped out at me when looking at your code. Below is what I am doing and it works. I am not using EJB though. Mine is in a Seam long running conversation.


                              // Listener for the image upload
                              public void listener(UploadEvent event) throws Exception {
                                UploadItem uploadItem = event.getUploadItem();
                                Photo photo = new Photo();
                                photo.setPhotoFileSize(uploadItem.getData().length);
                                photo.setPhotoFileName(uploadItem.getFileName());
                                photo.setPhotoContentType(uploadItem.getContentType());
                                photo.setData(uploadItem.getData());
                                photo.setPhotoPosition(item.getPhotos().size() + 1);
                                item.setPhoto(photo);
                                uploadsAvailable--;
                                photo = null;
                              }
                              



                              <rich:fileUpload fileUploadListener="#{create_listing.listener}"
                                maxFilesQuantity="#{create_listing.uploadsAvailable}" id="upload"
                                immediateUpload="#{create_listing.autoUpload}"
                                acceptedTypes="jpg, gif, png, bmp"
                                allowFlash="#{create_listing.useFlash}" listWidth="752px"
                                listHeight="85px" autoclear="#{create_listing.autoClear}">
                                <a:support event="onuploadcomplete" reRender="photoPanel" />
                              </rich:fileUpload>
                              



                              • 12. Re: RichFaces fileUpload
                                kem

                                Thanks Brian,


                                I've read somewhere there is duplication of jar files. This seams to be responsible for this problem. But I can't figure out which one.
                                May be I should restart my project without ejb.


                                Is there any reason for not using ejb?


                                khalil

                                • 13. Re: RichFaces fileUpload
                                  boomerbrian.brian.abston.featurenotbug.com

                                  There are several reasons for not using EJB. If you aren't using any of the EJB specific technologies I would recommend just using POJOs.

                                  • 14. Re: RichFaces fileUpload
                                    sappo

                                    One reason might be to deploy your project as war file instead of ear. This will enable hot deploment for java classes in src/action if the application server is started in debug mode.

                                    1 2 Previous Next